Chapter 6- String Manipulation Flashcards
Raw string
> > > print(r’That is Carol's cat.’)
That is Carol's cat.
The r is inside the printer argument but outside of strong
Indexing strings
>>> spam = 'Hello world!' >>> spam[0] 'H' >>> spam[4] 'o'
Slicing strings
> > > spam = ‘Hello world!’
fizz = spam[0:5]
fizz
‘Hello’
In and not in strings
>>> 'Hello' in 'Hello World' True >>> 'Hello' in 'Hello' True >>> 'HELLO' in 'Hello World' False >>> '' in 'spam' True >>> 'cats' not in 'cats and dogs' False
Upper()
Lower()
Converts the entire strong into upper or lower >>> spam = 'Hello world!' >>> spam = spam.upper() >>> spam 'HELLO WORLD!' >>> spam = spam.lower() >>> spam 'hello world!'
Important about upper() and lower()
Also how do I adjust for users inputting
Great GrEat and great
They do not change the string value Unless you have an assignment But the return is still a string print('How are you?') feeling = input() if feeling.lower() == 'great': print('I feel great too.') else: print('I hope the rest of your day is good.')
IsUpper()
IsLower()
Returns a boolean value of true or false
return a Boolean True value if the string has at least one letter and all the letters are uppercase or lowercase, respectively.
»> ‘abc12345’.islower()
True
Startswith()
Endswith()
Return True. They are Boolean
Split()
The split() method does the opposite: It’s called on a string value and returns a list of string
By default, the string ‘My name is Simon’ is split wherever whitespace characters such as the space, tab, or newline characters are found
Join()
The join() method is useful when you have a LIST of strings that need to be joined together into a single string value. The join() method is called
> > > ’, ‘.join([‘cats’, ‘rats’, ‘bats’])
‘cats, rats, bats’
‘ ‘.join([‘My’, ‘name’, ‘is’, ‘Simon’])
‘My name is Simon’
‘ABC’.join([‘My’, ‘name’, ‘is’, ‘Simon’])
‘MyABCnameABCisABCSimon’
ABC’.join([‘My’, ‘name’, ‘is’, ‘Simon’])
It will join the list with the value called as the middle point
MyABCnameABCisABCSimon
Remember that join()
Remember that join() is called on a string value and is passed a list value
Split() at something else besides white space
What happens to the value you set as the delimiter
> > > ‘MyABCnameABCisABCSimon’.split(‘ABC’)
[‘My’, ‘name’, ‘is’, ‘Simon’]
‘My name is Simon’.split(‘m’)
[‘My na’, ‘e is Si’, ‘on’]
It gets deleted
Spam.split(‘\n’)
Will split everywhere there is a new line
Don’t confuse this with white space
Rust()
ljust()
The rjust() and ljust() string methods return a padded version of the string they are called on, with spaces inserted to justify the text. The first argument to both methods is an integer length for the justified string.
The argument is how long you want the new string to be