Basics · Lesson 8 of 27
String methods
Strings come with built-in tools called methods. You use them with a dot.
text = " Hello World "
print(text.strip()) # "Hello World" removes spaces at both ends
print(text.upper()) # " HELLO WORLD "
print(text.lower()) # " hello world "
print(text.replace("World", "Python"))
print("a,b,c".split(",")) # ['a', 'b', 'c']
print("hello".count("l")) # 2
print("hello".startswith("he")) # Truesplit() with nothing inside splits on spaces, which is great for counting words:
print(len("I love Python".split())) # 3Methods don't change the original string. text.upper() gives you a new string; text stays the same.
Your task
text = " Learn Python Daily " is given. Print, one per line:
textwithout the spaces at the ends- that stripped text in UPPER case
- the stripped text with
Dailyreplaced byToday - the number of words in
text