Strings Flashcards
True or False: You have to use double quotes for strings in Python
False - you can use single or double quotes
How would you slice a string to get only certain letters in a string?
s = ‘abc’
s[0:2]
result = ‘ab’
True or False: strings are immutable in Python and cannot be reassigned like lists
True - any string modification will result in the creation of a new string, thus O(n) time
How can you add a letter to the end of a string in Python?
s = ‘abc’
s += ‘d’
This will result in a new string of O(n) time complexity
How would you add the values ‘123’ and ‘456’?
n = int(‘123’)
m = int(‘456’)
result = n + m
How would you create a string from 123 and 456
n = str(123)
m = str(456)
newString = n + m
What is the built-in function to get the ASCII value of a character in Python?
ord(“a”)
Result is 97
How can we combine strings using a delimiter in Python?
”“.join(strings)
This is a delimiter because it creates a new string with all other values included