String and methods in Python and practice questions
split()
maxsplit parameter
.split(maxsplit=1)
This maxsplit parameter determines max number of splits to perform. Default is -1 but if you set value like 1, split operation will be done only once and hence, maxsplit (literally). So it will create at most 2 substrings. If I have a string like
hey = "I love eating pizza and pasta."
print(hey.split()) # Output: ['I', 'love', 'eating', 'pizza', 'and', 'pasta.']
print(hey.split(maxsplit=1)) # Output: ['I', 'love eating pizza and pasta.']
strip()
This, unlike split(), removes any leading or trailing whitespaces in the string like
String sample = " hola "
replace()
If there is a certain char I want to replace or even remove, we can use .replace() like
string.replace(“some char you want to replace/remove”, “”)