Strings Flashcards
Convert string to uppercase
str.upper()
Convert string to lower case
str.lower()
Check string contains substring
‘substring’ in str
Length of string
len(str)
Replace substring with new substring
str.replace(‘old’, ‘new’)
Split string into list
str.split(separator)
Convert string to list
list(str)
Extract substring from string
str[start:end]
Start inclusive, end exclusive
Remove leading and trailing white spaces
str.strip()
Check if string starts or ends with substring
str.startswith(‘Hello’)
str.endswith(‘World’)
Split string around separator
str.partition(‘separator’)
Add 0s on the left of string
str.zfill(5)
Makes string length 5, adding 0s to make it to 5
Reverse string
str[::-1]
Count frequency of characters in string
from collections import Counter
Counter(string)