← Python for Beginners
0 of 27 lessons done0%

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")) # True

split() with nothing inside splits on spaces, which is great for counting words:

print(len("I love Python".split()))   # 3

Methods 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:

  1. text without the spaces at the ends
  2. that stripped text in UPPER case
  3. the stripped text with Daily replaced by Today
  4. the number of words in text