Python String Methods & f-strings

Format and transform text.

✍️ 3 exercises + code challenge in this lesson

Useful string methods (each returns a new string):

  • upper(), lower(), title(), capitalize()
  • strip(): remove spaces at both ends
  • replace(old, new)
  • split(sep) and "sep".join(list)
  • find(), count(), startswith(), endswith()

f-strings

Put an f before the string and insert values with {}: f"{name} is {age}". Format numbers with {price:.2f} or {n:,}.

Key Takeaways: Python String Methods & f-strings

Mastering Python String Methods & f-strings helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python String Methods & f-strings Code Example

Python
text = "  learn python at codesintech  "
clean = text.strip()

print(clean.title())
print(clean.upper())
print(clean.replace("python", "SQL"))
print(clean.split(" "))
print("-".join(["2026", "10", "22"]))
print(clean.count("o"), clean.startswith("learn"))

name, price, users = "Course", 4999.5, 1250000
print(f"{name} costs Rs {price:.2f} and has {users:,} users")
Free sign-in required · Google login available

Python String Methods & f-strings Practice Exercises

Check what you learned in “Python String Methods & f-strings”.

/ 3 solved
Fill in the blank

Create an f-string.

"Hello {name}"
✓ Correct! Not quite. Try again.
Multiple choice

Which method splits "a,b,c" into a list?

✓ Correct! Not quite. Try again.
Code challenge

Use an f-string to print "Ali is 30 years old".

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services