Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.

Similar presentations


Presentation on theme: "Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence."— Presentation transcript:

1 Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence of one character strings; includes the empty string "" Operators: +concatenation – join 2 strings *repetition – repeat string x times Examples: "cat"+" "+"dog"  "cat dog" ("---"+"|")*3  "---|---|---|"

2 Strings … are a sequence Since strings are a sequence and since for loops iterate over a sequence, for item in sequence: strings and for loops work well together. name = "Laurier" # create string for char in name: # char is not a keyword print ("{}".format(char)) for loops are ideal when you need to look at every character in a string

3 Write a function to remove spaces in a phrase. e.g. “some random words ” #main phrase_orig = input("Enter phrase: ") phrase_nosp= remove_sp (phrase_orig) print(phrase_nosp)

4 Strings … items have an index A sequence is ordered by position access items by their position or index name = "Laurier " u = 'Wilfrid Laurier University' print("{}{}{}{}".format(u[0], u[1], u[2], u[3])) print("{}{}{}{}".format(u[-1], u[-2], u[-3], u[-4]))

5 Strings … slicing Slicing is a general form of indexing that extracts a slice (section) in one step. =>typical form is x[start:limit] ≡ everything in x starting at index start up to but not including index limit univ = 'Wilfrid Laurier University ' print("{}".format(univ[8:15])) print("{}".format(univ[-11:-1])) print("{}".format(univ[-11:len(univ)]))

6 Strings … slicing  general form is x[start:limit:increment] ≡ everything in x starting at index start up to but not including index limit in steps of increment where: start – defaults to 0 limit – defaults to len(x) increment – defaults to 1 if start is not less than limit, return a null string, "" univ = 'Wilfrid Laurier University ' e.g. univ[:5] univ[:] univ[::3]

7 Strings …immutability Strings are immutable: they cannot be changed in place, or you cannot overwrite the values in a string name = 'Carol' name[0] = 'B'<= causes error #'str' object does not support item assignment but you can create a new string object name = 'Carol' name = 'B' + name[1:]

8 Write a function to parse a name into first and last names. #main full_name = input("Enter name ") first, last = parse_name (full_name) print(first) print(last)

9 Write a program to check the area code of a phone number. # main num = input ("Enter phone number with area code ") acode = input ("Enter area code ") ok = area_check(num, acode)

10 Common String Operations OperationResult compare operations: =, > Compares string1 operator string2 -> comparison based on ASCII value -> equivalent to dictionary order for numbers and letters x in string x not in string True if x is equal to item in string; ** False if x is equal to item in string item1 + item2concatenation item * nconcatenate n copies of item string[ i ]item i of string, numbering starts at 0 string[ i:j ] string[ i:j:k ] slice of string from i to j slice of string from i to j with step k len(string)length of string ** Can be used for substring testing, e.g. “gl” in “glow”.


Download ppt "Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence."

Similar presentations


Ads by Google