Lesson 9 of 14 · 9 min

Working with text

Strings have many built-in methods. They never change the original string; they return a new one.

s = "  Hello, Harare  "

s.strip()           # "Hello, Harare"     remove spaces at both ends
s.lower()           # "  hello, harare  "
s.upper()           # "  HELLO, HARARE  "
s.replace("H", "J") # "  Jello, Jarare  "
"harare".capitalize()   # "Harare"
"Harare" in s       # True
s.count("a")        # 3

Indexing and slicing

Strings can be indexed and sliced just like lists:

word = "Python"
word[0]      # "P"
word[-1]     # "n"
word[1:4]    # "yth"
word[::-1]   # "nohtyP"   a step of -1 reverses

split and join

"a b  c".split()           # ["a", "b", "c"]     splits on any spaces
"2026-11-16".split("-")    # ["2026", "11", "16"]
"-".join(["a", "b", "c"])  # "a-b-c"

Multiplying a string repeats it: "ab" * 3 is "ababab".

Your task

  1. clean_name(raw): remove extra spaces and give each word one capital letter. " tARIRO moyo " becomes "Tariro Moyo".
  2. is_palindrome(text): True if the text reads the same backwards, ignoring case and spaces.
  3. mask_phone(number): replace every digit except the last 3 with *.
main.py
def clean_name(raw):
    pass


def is_palindrome(text):
    pass


def mask_phone(number):
    pass


print(clean_name("  rUDO  chikwanha "))
print(is_palindrome("Step on no pets"))
print(mask_phone("0779876543"))

Python 3.14 runs right here in your browser. The first run downloads about 10 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…