strings Flashcards
check each word from capital
‘The Hilton’.istitle()
check string contains another string
‘plane’ in ‘The worlds fastest plane’
find index of substing or -1
‘The worlds fastest plane’.find(‘plane’)
find index or ValueError
‘The worlds fastest plane’.index(‘car’)
‘the happiest person in the whole wide world.’.index(‘the’,10,44)
how many substings
‘The first president of the organization..’.count(‘o’)
make first symbol capital
‘florida dolphins’.capitalize()
f-sting exaple
f’Hello. My name is {name} and I like {food}.’
if string contains only numbers
‘80000’.isnumeric()
if only lowercase
‘all lower case’.islower()
all symbols is ascii
‘A’.isascii()
change register
sentence. upper() #=> ‘THE CAT IN THE HAT’
sentence. lower() #=> ‘the cat in the hat’
if only uppercase
‘Toronto’.isupper() #=> False
‘TORONTO’.isupper() #= True
if only alphabet characters
‘One1’.isalpha() #=> False
‘One’.isalpha() #=> True
if number and alphabet characters
‘Ten10’.isalnum() #=> True
‘Ten10.’.isalnum() #=> False
remove spaces
string = ‘ string of whitespace ‘
string. lstrip() #=> ‘string of whitespace ‘
string. rstrip() #=> ‘ string of whitespace’
string. strip() #=> ‘string of whitespace’
check if the string starts or ends with substring
city = ‘New York’
city. startswith(‘New’) #=> True
city. endswith(‘N’) #=> False
if only spaces
’‘.isspace() #=> False
‘ ‘.isspace() #=> True
‘ ‘.isspace() #=> True
‘ the ‘.isspace() #=> False
fist character to uppercase
‘once upon a time’.title() #=> ‘Once Upon A Time’
translate example
mapping = str.maketrans("abcs", "123S") "abc are the first three letters".translate(mapping) #=> '123 1re the firSt three letterS'
find from right
story = ‘The price is right said Bob. The price is right.’
story.rfind(‘is’)