Python Learning Series · @onlysamarrr · Day 6 / 30

Day 6: String Methods

Day 6 covers string methods like strip(), upper(), title(), split() and replace(). Strings are immutable, so every method returns a new string instead of changing the original.

Phase: Beginner · Reels: 26/450 – 30/450

Quick Answer

Day 6 covers string methods like strip(), upper(), title(), split() and replace(). Strings are immutable, so every method returns a new string instead of changing the original.

What You Will Learn

Code

s = " arhan KHAN "
print(s.strip())
print(s.strip().title())
print(s.upper().strip())
print("a,b,c".split(","))
print("-".join(["a", "b"]))
print("tea".replace("t", "T"))
print(" Arhan_Dev ".strip().lower())

Output

arhan KHAN
Arhan Khan
ARHAN KHAN
['a', 'b', 'c']
a-b
Tea
arhan_dev

Code Explained, Line by Line

  1. s.strip() removes spaces from both ends of a string.
  2. .title() capitalises the first letter of every word. .upper() makes the whole string capital.
  3. .split(",") breaks text at commas and returns a list of parts.
  4. "-".join(["a", "b"]) joins list items with - between them, giving "a-b".
  5. .replace("t", "T") returns a new string with the letter replaced.
Common Mistake: Calling s.upper() alone does not change s, because strings are immutable. You must write s = s.upper(). Trying s[0] = "A" raises a TypeError.

Real-Life Use

Cleaning usernames, emails and form input uses strip(), lower() and title() in almost every app.

Practice Task: Take your name with extra spaces, strip it, then capitalise it with title().

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What does " hi ".strip() return?

Q2. After s = "hi" and s.upper(), what is s?

Q3. What does "a,b".split(",") return?

Today's 5 Reels (from the 90-Day Plan Excel)

26/450Conceptupper, lower, title
27/450Live demostrip, replace, split demo
28/450Common mistakeStrings immutable hain
29/450QuizQuiz: naam ko capitalize karo
30/450Interview / real lifeReal life: username cleaner
← Day 5Day 7 →