3/8/19 Py Python Strings Python strings are immuatable: s = ‘abc’ s[2] = ‘d’ s = ‘abd’ These don’t change the string ‘abc’ s = s[:len(s)-1] + ‘d’ they reassign the string variable s CS 224 Introduction to Python Spring 2019 Class #18: Strings Ac Accessor and Slices Me Methods Python provides a broad array of string operations. s = ’A diamond necklace played the pawn’ Because strings are immuatable, the operations do not modify the string. print(s[5]) # prints m print(s[len(s)-1] # prints n Most methods fall into one of the following categories: print(s[-1]) # prints n • Return a new string print(s[-2]) # prints w • Return a Boolean print(s[10:18]) # prints necklace • Return an int t = s[19:] # t is ‘played the pawn’ • Return a tuple or list u = s[:9] # u is ‘A diamond’ print(s[-4:]) # prints pawn v = s[:-16] # v is ‘A diamond necklace’ 1
3/8/19 Me Methods that return a string Upper and lower case Up • s.capitalize() s.lower() • s.lower() returns an all lower case conversion of s • s.upper() • s.join(t) s.upper() • s.replace(old, new) returns an all upper case conversion of s • s.strip() • s.rstrip() s.capitalize() returns s with first character capitalized Examples: upper and lower case Ex Jo Join s = ‘Hand in hand some drummed along’ s.join(t) s.lower() t is an iterable of strings returns ‘hand in hand some drummed along’ returns a concatenation of strings in t with s.upper() string s as a separator returns ‘HAND IN HAND SOME DRUMMED ALONG’ s.lower().capitalize() returns ‘Hand in hand some drummed along’ ‘123’.capitalize() returns ‘123’ 2
3/8/19 Ex Examples: join Re Replace s = ‘ ’ s.replace(old, new) t = [‘this’, ‘is’, ‘a’, ‘test’] old is a string new is a string s.join(t) # t is an iterable of strings returns ‘this is a test’ returns a string in which occurrences of old have been replaced with new ‘,‘.join(t) returns ‘this,is,a,test’ ‘ la ‘.join(t) returns ‘this la is la a la test’ Examples: replace Ex ce St Strip s.strip() s = ‘this is a test’ returns s without leading and trailing whitespace s.replace(‘is’, ‘was’) s.rstrip() returns ‘thwas was a test’ returns s without trailing whitespace # unintended consequences s.strip(t) s.replace(‘ is’, ‘was’) t is a string returns ‘thiswas a test’ returns s without leading and trailing chars in t # d’oh! s.rstrip(t) s.replace(‘ is’, ‘ was’) returns s without trailing chars in t returns ‘this was a test’ 3
3/8/19 Ex Examples: strip Ex Examples: strip s = ‘ Early one mornin the sun was shinin \n’ s = ‘de do do do de da da da’ s.strip() s.strip(‘a’) returns ‘Early one mornin the sun was shinin’ returns ‘de do do do de da da d’ s.rstrip() s.strip(‘ad’) returns ‘ Early one mornin the sun was shinin’ returns ‘e do do do de da da ’ s.rstrip(‘a d’) returns ‘de do do do de’ Me Methods that return a Boolean Ex Examples: st startswith & & ends endswith s = ‘She loves you, yeah, yeah, yeah’ • s.startswith(prefix) • s.endswith(suffix) s.startswith(‘She lov’) • s.isalnum() returns True • s.isalpha() • s.isdigit() s.startswith(‘she’) • s.isupper() returns False • s.islower() • s.isspace() s.lower().startswith(‘she’) • s.istitle() returns True s.endswith(‘ah, yeah’) returns True 4
3/8/19 Ex Examples: is isaln lnum, , is isalp lpha, , & is isdig igit it Ex Examples: is isaln lnum, , is isalp lpha, , & is isdig igit it s = ‘Waiting for the break of day’ s = ‘Waiting for the break of day’ t = ’25 or 6 to 4’ t = ’25 or 6 to 4’ u = ‘314159’ u = ‘314159’ s.isalnum() t.isalpha() returns True returns False s.isalpha() t.isdigit() returns True returns False t.isalnum() u.isdigit() returns True returns True Examples: is Ex islo lower & & is isupper Ex Examples: is islo lower & & is isupper s = ‘Waiting for the break of day’ s = ‘Waiting for the break of day’ t = ’25 or 6 to 4’ t = ’25 OR 6 TO 4’ u = ‘314159’ s.isupper() s.islower() returns False returns False s.upper().isupper() t.islower() returns True returns True t.isupper() u.islower() returns True returns False 5
3/8/19 Ex Examples: is isspace & & is istit itle le Me Methods that return an in int s = ‘ \t \n’ • s.count(sub) t = ’Let Him Run Wild’ • s.find(sub) • s.rfind(sub) s.isspace() • s.index(sub) returns True • s.rindex(sub) t.istitle() returns True t.upper().istitle() returns False Count Co Ex Examples: count s = ‘de do do do de da da da’ s.count(sub) sub is a string s.count(‘da’) returns 3 returns the number of occurrence of sub in s s.count(‘da ’) returns 2 s.count(‘do do’) returns 1 6
3/8/19 Fi Find Ex Examples: find and rf rfind s = ‘De do do do de da da da’ s.find(sub) sub is a string s.find(‘do’) returns 3 returns index of first occurrence of sub in s or -1 if not found s.rfind(‘do’) returns 9 s.rfind(sub) sub is a string s.find(‘de’) returns 12 returns index of last occurrence of sub in s or -1 if not found s.find(‘Do’) returns -1 In Index Ex Examples: index and ri rindex s = ‘De do do do de da da da’ s.index(sub) sub is a string s.index(‘do’) returns 3 returns index of first occurrence of sub in s or gives an error if not found s.rindex(‘do’) returns 9 s.rindex(sub) sub is a string s.index(‘de’) returns 12 returns index of last occurrence of sub in s or gives an error if not found s.index(‘Do’) Crash and burn – should have used find 7
3/8/19 Me Methods that return a tuple or list Pa Partition s.partition(sep) • s.partition(sep) first occurrence of sep partitions s • s.rpartition(sep) • s.split(sep) returns tuple: • s.rsplit(sep) • s.splitlines() (s_up_to_sep, sep, s_after_sep) s.rpartition(sep) last occurrence of sep partitions s returns tuple: (s_up_to_sep, sep, s_after_sep) Examples: partition Ex Ex Examples: partition s = ‘de do do do de da da da’ s = ‘Help me Rhonda help help me Rhonda’ s.partition(‘do’) s.partition(‘Rhonda’) returns (’de ‘, ‘do’, ‘ do do de da da da’) returns (’Help me ’, ’Rhonda’, ‘ help help me Rhonda’) s.rpartition(‘do’) s.rpartition(‘Rhonda’) returns (’de do do ‘, ‘do’, ’ de da da da’) returns (’Help me Rhonda help help me ‘, ‘Rhonda’, ‘’) s.partition(‘do do do’) returns (‘de ‘, ‘do do do’, ‘ de da da da’) s.rpartition(‘do do do’) returns (‘de ‘, ‘do do do’, ‘ de da da da’) 8
3/8/19 Spl Split Ex Examples: split s = ‘de do do do de da da da’ s.split(sep [, max]) each occurrence of sep partitions s, up to max times s.split(‘ ’) or s.split() returns [’de‘, ‘do’, ‘do’, ‘do’, ‘de’, ‘da’, ‘da’, ‘da’] returns list of tokens s.rsplit(‘ ’) or s.rsplit() s.rsplit(sep [, max]) returns [’de‘, ‘do’, ‘do’, ‘do’, ‘de’, ‘da’, ‘da’, ‘da’] each occurrence of sep partitions s, up to max times s.split(‘ ’, 3) returns list of tokens returns [‘de‘, ‘do’, ‘do’, ‘do de da da da’) split and rsplit are same unless occurrences > max s.rsplit(‘ ‘, 3) returns (‘de do do do de’, ‘da’, ‘da’, ‘da’] Ex Examples: split Spl Splitlines nes s = ’12,657,489,306’ t = ‘de do do do de da da da’ s.splitlines() splits s at newlines; consumes the newlines s.split(‘,’) returns [’12’, ‘657’, ‘489’, ‘306’] returns list of lines t.split(‘do’) returns [‘de ‘, ‘ ’, ‘ ’, ‘ de da da da’) s.rsplit(‘do‘, 2) returns (‘de do ‘, ’ ’, ‘ de da da da’] 9
3/8/19 Ex Examples: sp splitl tlines s = ‘A diamond necklace played the pawn Hand in hand some drummed along To a handsome man and baton Bygone, bygone’ s.splitlines() returns [‘A diamond necklace played the pawn’, ’Hand in hand some drummed along’, ’To a handsome man and baton’, ‘Bygone, bygone’] 10
Recommend
More recommend