Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

Similar documents
Chapter 8: More About Strings. COSC 1436, Summer 2018 Dr. Zhang 7/10/2018

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

String Processing CS 1111 Introduction to Programming Fall 2018

MULTIPLE CHOICE. Chapter Seven

Introduction to String Manipulation

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

UNIT-III. All expressions involving relational and logical operators will evaluate to either true or false

Introduction to: Computers & Programming: Strings and Other Sequences

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Python Tutorial. Day 1

Composite)Types. Types)Of)Variables. Addresses)And)References)(2) Addresses)And)References. Small)Example)Programs)Using)Strings

18.1. CS 102 Unit 18. Python. Mark Redekopp

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

COMP1730/COMP6730 Programming for Scientists. Strings

Python Review IPRE

COLLEGE OF ENGINEERING, NASHIK-4

Python Review IPRE

PYTHON MOCK TEST PYTHON MOCK TEST III

Topic 7: Lists, Dictionaries and Strings

Strings are actually 'objects' Strings

Advanced Python. Executive Summary, Session 1

CS1 Lecture 11 Feb. 9, 2018

Module 3: Strings and Input/Output

Introduction to Computer Programming for Non-Majors

Chapter 10 Characters, Strings, and the string class

Programming in Python

Python. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

DECODE SPECIAL OPERATOR, FORMAT OPERATOR CONTENTS TRIPLE QUOTES. IS a-to-z METHODS REPLACE L E N G T H E X P A N D T A B S ENC0D3

Strings, Lists, and Sequences

Structured programming

Strings. Chapter 6. Python for Everybody

IPSL python tutorial: some exercises for beginners

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introductory Linux Course. Python I. Martin Dahlö UPPMAX. Author: Nina Fischer. Dept. for Cell and Molecular Biology, Uppsala University

Agenda. Strings 30/10/2009 INTRODUCTION TO VBA PROGRAMMING. Strings Iterative constructs

Exam 3. 1 True or False. CSCE 110. Introduction to Programming Exam 3

Computing with Numbers

Zero Buffer Documentation

Chapter 10: Characters, C- Strings, and More About the string Class

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Introduction to Python for Plone developers

CS106X Handout 04 Autumn 2009 September 25 th, 2009 C++ Strings

Python: common syntax

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

Strings and Library Functions

CS 102 Lab 3 Fall 2012

CS106X Handout 04 Winter 2018 January 8 th, 2018 C++ Strings

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

today cs3157-fall2002-sklar-lect05 1

1 Strings (Review) CS151: Problem Solving and Programming

Python for Informatics

Introductory Linux Course. Python I. Pavlin Mitev UPPMAX. Author: Nina Fischer Dept. for Cell and Molecular Biology, Uppsala University

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The second statement selects character number 1 from and assigns it to.

CS1 Lecture 9 Feb. 5, 2018

Introduction to Computer Programming for Non-Majors

Script language: Python Data structures

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

System Design and Programming II

Compound Data Types 1

Lists How lists are like strings

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

Jarek Szlichta

String Objects: The string class library

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

def order(food): food = food.upper() print( Could I have a big + food + please? ) return fresh + food

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Strings. Looping. dna = 'ATGTAGC' print(dna) In [1]: ATGTAGC. You can loop over the characters of a string using a for loop, as we saw on Tuesday:

Fundamentals of Computer Programming Using C

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

EE 355 Unit 17. Python. Mark Redekopp

Python Companion to Data Science

CIS192 Python Programming

Fundamentals of Programming. Lecture 11: C Characters and Strings

ISE 101 Introduction to Information Systems. Lecture 3 Objectives: While loops Strings

COMP 364: Classes, Objects, and Names

Scientific Programming in C V. Strings

Python - 2. Jim Eng

Python. Executive Summary

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Python Programming Exercises 3

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CS1 Lecture 12 Feb. 11, 2019

Unit 2. Srinidhi H Asst Professor

Composite Types. You will learn how to create new variables that are collections of other entities. Types Of Variables.

C: How to Program. Week /May/28

CS106B Handout #5 Winter January 9, 04

CPS 109 Lab 2 Alexander Ferworn Updated Fall 05. Ryerson University. School of Computer Science CPS109. Lab 2

Composite Types. You will learn how to create new variables that are collections of other entities. Types Of Variables.

Control structure: Repetition - Part 1

Transcription:

Chapter 8 Working with Sequences: Strings and Lists Section 8.1 and 8.2 Bonita Sharif 1

Sequences A sequence is an object that consists of multiple data items These items are stored consecutively Examples A string This is a string Ali list numbers = [ a, b, c ] 2

Types of Sequences Strings Sequences that hold characters Strings are immutable Lists Sequences that hold various types of data Dynamic data items Lists are mutable 3

Iterating over a String A for loop is used for iterating over a string of characters General Form for variable in string: statement t t : Examples sentence = The quick brown fox jumps over the lazy dog for char in sentence: print char See count_ts.py for another example 4

String Indexing A string s s characters can be referenced using an index. The index is a number that denotes the position of the character in the string Id Indexing starts t from 0 It is given inside square brackets. [ ] 5

String Indexing Examples my_string = I love Python Programming ch = my_string[8] print ch # negative index points to characters relative to the end of the string print my_string[-1] print my_string[ string[-4] # causes an IndexError exception print my_string[25] # Prints the length of the string print len(my_string) 6

IndexError Exception This exception occurs if you try to use an invalid index in a string. 7

String Length You can use the len() function to determine the length of a string Example strlen = len(my_string) The len() function is used in while loops to determine the end of a string. Example index = 0 while index < len(my_string) statement index += 1 8

Strings are Immutable Concatenation doesn t really change the original string What really happens? You cannot change a particular character in a string my_ string[1] = i # error 9

String Slicing A string slice consists of part of a string String slice is also called a substring To slice a string you specify the start and end in [start:end] There are several ways you can do this If start is left out, it defaults to 0 If end is left out, it defaults to string length 10

String Slicing If both indices are left out, then the first index defaults to 0 and the second index defaults to the length of the string Step value in string slicing It causes characters to be skipped in the string. Invalid indices do not raise exceptions. If start is > than end it returns an empty string If start or end dindex does not exist 11

Examples full_name = Patty Lynn Smith middle_name = full_name[6:10] first_name = full_name[:5] last_name = full_name[11:] my_string1 = full_name[:] my_string2 = full_name[0: len(full_name)] my_string3 = full_name[0:16:3] step value 12

Book Examples generate_login.py login.py 13

String Containment You can test to see if a string contains another string using the in and not in operators. General format string1 in string2 string1 not in string2 14

Example word = fox sentence = The quick brown fox jumped if text in sentence: print Found else: print Not Found 15

String Methods A method is a function that belongs to an object. It does some operations on the object. In this case the object is a string. String Methods to Test strings Modify strings Search and replace substrings 16

String Testing Methods isalnum(): isalpha() isdigit() il islower() isspace() isupper() (string_test.py) test 17

String Modification Methods lower() lstrip(), lstrip(char) rstrip(), rstrip(char) strip(), strip(char) h upper() swapcase() 18

Case Insensitive Comparisons continue = y while continue.lower() == y : do something continue = raw_input( Do you want to continue? Enter y or n: ) 19

Searching and Replacing Methods endswith(substring) find(substring) replace(old, new) startswith(substring) ih( i 20

An Example Ask the user to enter a password Validate the password to meet the following criteria Passwords must be At least 7 characters long Contain at least 1 uppercase letter Contain at least 1 lowercase letter Contain at least 1 numeric digit See login.py and validate_password.py py 21

The Repetition Operator Besides multiplication, * also denotes the repetition operator. When used between a string and a number it denotes the repetition operator. Example my_string ti = b * 5 See repetition_operator.py 22

Exercises string = I am a string. Hello string.rstrip( Helo. ) # What is the content of variable string? string.upper() # What is the content of variable string? string = string.rstrip( Helo. ) # What is the content of variable string? 23

Converting a String to a List string = I am a string mylist = string.split() split() The result will be a list containing i 4 elements [ I, am, a, string ] 24