String Methods
Strings have many methods and each one returns a new string, because strings cannot be changed. Common ones: upper, lower, title, strip, replace, split, join, find, startswith, endswith and count.
Example
text = " learn python "
print(text.strip())
print(text.strip().title())
print(text.replace("python", "coding"))
print("a,b,c".split(","))
print("-".join(["x", "y", "z"]))
print("banana".count("a"))
print("hello".startswith("he"))Result
learn python Learn Python learn coding ['a', 'b', 'c'] x-y-z 3 True
Exercise
Remove the spaces at both ends.
print(" hi ".())
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.