Code / Python for Beginners / Unit 5: Functions, text and your project

Working with text टेक्स्ट के साथ काम

Lesson 13 of 14 · about 16 minutes · strip, upper, lower, replace, count, split, join and isdigit.

⚡ 0 XP

Step 1 of 2

Clean up messy text

N0A1M2A3S4T5E6"NAMASTE".length → 7

People type messy things: extra spaces, wrong capitals. Strings have methods to clean them. Each one gives you a new string (the old one is unchanged):

  • " asha ".strip() → "asha" (spaces removed at both ends)
  • "asha".upper() → "ASHA", "ASHA".lower() → "asha"
  • "asha verma".title() → "Asha Verma"
  • "chai".replace("i", "y") → "chay"
  • "banana".count("a") → 3
  • len("asha") → 4

You can chain them: raw.strip().title().

Example
raw = "   rAHUL dravid  "
name = raw.strip().title()
print(name)
print(name.upper())
print(name.lower().count("a"))

🎯 Challenge: Clean the name +20 XP

Read a messy name. Make name the clean version with .strip().title(). Then print three lines. For aSHA verma they are:

  • Name: Asha Verma
  • Shout: ASHA VERMA
  • Letters a: 3 (count the letter a in the lowercase name)

Try twice to unlock the solution (0/2)

main.pyCtrl ⏎
Output

What your program prints appears here.

Checklist

Press Run to check your code.

  • name is cleaned
  • Asha Verma: all three lines
  • Works for Ravi Kumar
Lesson notes (text version)

1. Clean up messy text

People type messy things: extra spaces, wrong capitals. Strings have methods to clean them. Each one gives you a new string (the old one is unchanged):

  • " asha ".strip() → "asha" (spaces removed at both ends)
  • "asha".upper() → "ASHA", "ASHA".lower() → "asha"
  • "asha verma".title() → "Asha Verma"
  • "chai".replace("i", "y") → "chay"
  • "banana".count("a") → 3
  • len("asha") → 4

You can chain them: raw.strip().title().

                raw = "   rAHUL dravid  "
name = raw.strip().title()
print(name)
print(name.upper())
print(name.lower().count("a"))
              

Challenge: Clean the name

Read a messy name. Make name the clean version with .strip().title(). Then print three lines. For aSHA verma they are:

  • Name: Asha Verma
  • Shout: ASHA VERMA
  • Letters a: 3 (count the letter a in the lowercase name)

2. split, join and isdigit

split cuts a string into a list of pieces:

  • "food,120".split(",") → ["food", "120"]
  • "chai samosa vada".split() → split on spaces

join does the opposite: "-".join(["a", "b"]) → "a-b".

isdigit() is True only when every character is a digit: "98765".isdigit() is True, "98a65".isdigit() is False. Great for checking input before you call int() on it.

Strings can be sliced like lists: num[:5] is the first five characters.

                words = "chai samosa vada".split()
print(words)
print(" + ".join(words))
print("98765".isdigit(), "98a65".isdigit())
              

Challenge: Mobile number checker

Read a mobile number. Remove the spaces with .replace(" ", "") and store it in num. If num has exactly 10 characters and num.isdigit(), print it as Valid: 98765-43210 (first 5, a dash, last 5). Otherwise print Invalid number.

Search by task, format or tool name.