Chapter 8: More About Strings COSC 1436, Summer 2018 Dr. Zhang 7/10/2018
Creating Strings The str Class s1 = str() # Create an empty string s2 = str("welcome") # Create a string Welcome Python provides a simple syntax for creating string using a string literal. For example, s1 = "" # Same as s1 = str() s2 = "Welcome" # Same as s2 = str("welcome") 2
>>> s = "Welcome" >>> len(s) 7 >>> max(s) o >>> min(s) W Functions for str 3
Index Operator [] 0 1 2 3 4 5 6 7 8 9 10 s P r o g r a m m i n g s[0] s[1] s[10]
The +, *, [ : ], and in Operators >>> s1 = "Welcome" >>> s2 = "Python" >>> s3 = s1 + " to " + s2 >>> s3 Welcome to Python >>> s4 = 2 * s1 >>> s4 WelcomeWelcome >>> s1[3 : 6] com >>> 'W' in s1 True >>> 'X' in s1 False
Negative Index >>> s1 = "Welcome" >>> s1[-1] e >>> s1[-3 : -1] om
The in and not in Operators >>> s1 = "Welcome" >>> "come" in s1 True >>> "come" not in s1 False >>>
More about strings Accessing the individual characters in a string for variable in string: statement statement etc Example: name = Juliet for ch in name: print(ch)
Foreach loop for ch in string: print(ch) for i in range(0, len(s), 2): print(s[i])
More about strings https://www.tutorialspoint.com/python/python_strings.htm Accessing values in a string var1 = 'Hello World!' var2 = "Python Programming" print ("var1[0]: ", var1[0] ) print ("var2[1:5]: ", var2[1:5]) When the above code is executed, it produces the following result var1[0]: H var2[1:5]: ytho
String operations-cont Indexing and IndexError Exceptions Each character in a string has an index that specifies its position in the string. An IndexError exception will occur if you try to use an index that is out of range for particular string. Example: city = Boston Index = 0 While index < 7 print(city[index]) index +=1
String operation - conti len function Get length of a string: length = len(city) city = Boston Index = 0 While index < len(city) print(city[index]) index +=1 String Concatenation Using + to concatenate firstname = Emily lastname= Yeager fullname = firstname + lastname pring(fullname) Output: Emily Yeager Strings Are Immutable
String operations-cont String Slicing to get a slice of a string String[start : end] Example: fullname= Emily Yeager firstname=fullname[0: 5] print(firstname) output: Emily
String Special Operators Assume string variable a holds 'Hello' and variable b holds 'Python', then Operator Description Example + Concatenation - Adds values on either side of the operator * Repetition - Creates new strings, concatenating multiple copies of the same string [] Slice - Gives the character from the given index [ : ] Range Slice - Gives the characters from the given range a + b will give HelloPython a*2 will give -HelloHello a[1] will give e a[1:4] will give ell in not in Membership - Returns true if a character exists in the given string Membership - Returns true if a character does not exist in the given string H in a will give 1 M not in a will give 1
Testing, Searching, and Manipulating Strings Testing Strings with in and not in String1 in string2 String1 not in string2 Example: Names = Bill Joanne Susane Chris Juan Katie if Pierre not in Names: print( Pierre was not found ) else print( Pierre is found )
Testing, Searching, and Manipulating Strings Conti- Method isalnum() isalpha() isdigit() islower() isspace() isupper() Description Returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise. Returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise. Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise. Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise. Returns true if the string contains only whitespace characters and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs(\t). Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise.
Testing, Searching, and Manipulating Strings Contin- String Modification Methods p354 lower() rstrip(char) lstrip() strip() lstrip(character) strip(char) rstrip() upper() Search and replace methods (p355) endswith(substring) find(substring) replace(old,new) startswith(substring) Returns true if the string ends with substring Returns lowest index in the string where substring is found. If not found, returns -1 Returns a coopy of the string with all insances of old replaced by new Returns true if the string starts with substring
Splitting a string http://www.jquery-az.com/python-split-string-methods-split-rsplit-and-splitlines-6- examples/ The split method returns a list containing the words in the string. Code example My_string = one two three four Word_list = () Print(Word_list) My_string.split Output: [ one, two, three, four ]
Lab and assignment Project discussion Lab: Chapter 8: exercise #1,#11 Review Question Assignment: Chapter 8: exercise #12 Due date: 7/12/2017