41 Python String Questions Flashcards
How would you confirm that two strings share the same identity?
The is operator returns True
animals = ['python', 'gopher'] more\_animals = animals
print(animals == more_animals) # True
print(animals is more_animals) # True
even_more_animals = [‘python’, ‘gopher’]
print(animals == even_more_animals) # True
print(animals is even_more_animals) # False
How would you check if each word in a str begins with a capital letter?
The istitle() func checks if each word is capitalized
print(‘The Hilton’.istitle()) # True
print(‘Kelvin waters’.istitle()) # False
Check if a string contains a specific substring
The in operator will return True if a string contains a substring
print(‘plane’ in ‘The worlds fastest plane’) # True
print(‘car’ in ‘The worlds fastest plane’) # False
Find the index of the first occurrence of a substring in a string
There are 2 different func that will return the starting index(), find() and index().
‘The worlds fastest plane’.find(‘plane’) #19
find() returns -1 if the substring is not found
‘The worlds fastest plane’.find(‘car’) # -1
index() will throw a valueError if occurance missing
‘The words fastest plane’.index(‘plane’) # True
‘The words fastest plane’.index(‘car’) # ValueError
Count the total number of chars in a str
len() will return the length of a string
len(‘The first president of the organization’) # 41
Count the number of a specific char in a string
count() will return the number of occurrences of a specific character
‘The first president of the organisation..’.count(‘o’) # 3
Capitalize the first character of a string
use the capitalize() to do this
‘florida dolphins’.capitalize() # Florida dolphins
What is an f-string and how do you use it?
using f-strings is similiar to using format() # F-strings are denoted by an f before the opening quote
name = 'Kelvin' food = 'lobster tails'
f’Hello. My name is {name} and I like {food}!’
Search a specific part of a str in a sunstring
index() can also be provided with optional start and end indices for searching within a larger string
‘the happiest person in the whole wide world.’.index(‘the’, 10, 44) # 23
Interpolate a variable into a string using format()
format() is similar to using an f-string, though less user friendly
difficulty = 'easy' thing = 'exam'
‘That {} was {}!’.format(thing, difficulty) # ‘That exam was easy!’
Check if a string contains only numbers
isnumeric() returns True if all characters are numeric
print(‘80000’.isnumeric()) # True
print(‘8.0000’.isnumeric()) # False punctuation is none numeric
print(‘900is00’.isnumeric()) # False
Split a string on a specific character
split() function will split a str on a given character or characters
‘This is great’.split(‘ ‘) # [‘This’, ‘is’, ‘great’]
‘not–so–great’.split(‘–’) # [‘not’, ‘so’, ‘great’]
Check if a string is composed of all lower case characters
islower() returns True only if all chars in a string are lowercase
‘all lower case’.islower() # True
‘aLL lower Case’.islower() # False
Check if the first character in a string is lowercase
This can be done by calling the previously func on the first index of a string
‘aPPLE’[0].islower() # True
Can an integer be added to a string in Python
No can not concatenate a string to an integer returns a TypeError
Reverse the string ‘hello world’
We can split the string into a list of char, reverse the list, then rejoin into a single string.
’ ‘.join(reversed(‘hello world’))