Home / Topic Library / Python / Strings
🐍 Python Topic Library

Python Strings — f-strings, Slicing & Every Useful Method

Text is the data type you'll touch most. Python's strings are immutable sequences with a superb toolkit — and f-strings make formatting genuinely pleasant.

⚡ Quick Answer

Create strings with quotes: name = "Ada". Format with f-strings: f"Hello {name}!". Slice with text[start:end]. Strings are immutable — methods return new strings, they never change the original.

§f-strings — modern formatting

Since Python 3.6, f-strings are the way to build text. Put any expression inside {}.

fstrings.py
name, score = "Ada", 95
print(f"{name} scored {score}!")        # Ada scored 95!
print(f"Next year: {score + 5}")        # expressions work
print(f"Pi ≈ {3.14159:.2f}")            # format: 3.14
print(f"{score:>8}")                    # right-align in 8 chars
print(f"{name=}")                        # debug: name='Ada'

§Indexing & slicing

slicing.py
word = "Python"
print(word[0])      # P   (first — counting starts at 0)
print(word[-1])     # n   (last)
print(word[0:3])    # Pyt (start included, end EXCLUDED)
print(word[2:])     # thon
print(word[::-1])   # nohtyP — reversed!

§The methods you'll actually use

methods.py
s = "  Learn Python at BitWithBite  "
print(s.strip())              # trim spaces both ends
print(s.lower())              # all lowercase
print(s.upper())              # ALL CAPS
print(s.replace("Python", "coding"))
print("python" in s.lower())  # True — membership test
print(s.strip().startswith("Learn"))   # True
print(s.count("t"))           # occurrences

§Splitting & joining

split_join.py
csv = "ada,grace,alan"
names = csv.split(",")          # ['ada', 'grace', 'alan']
print(" & ".join(names))        # ada & grace & alan

sentence = "the quick brown fox"
words = sentence.split()        # splits on any whitespace
print(len(words))               # 4

§Immutability — strings never change

immutable.py
name = "ada"
# name[0] = "A"        # TypeError! strings can't be edited in place
name = name.capitalize()  # make a NEW string instead
print(name)               # Ada

§Common Mistakes to Avoid

🚨 Watch out for these
  • Forgetting methods return new stringss.upper() alone does nothing visible — assign it: s = s.upper()
  • Off-by-one in slices — the end index is excluded: "Python"[0:3] is 'Pyt', not 'Pyth'
  • Concatenating str + int"age: " + 12 is a TypeError — use f-strings or str(12)

§Frequently Asked Questions

What is an f-string in Python?

A string literal prefixed with f that evaluates expressions inside {} — f"Hello {name}" — the modern, fastest way to format text (Python 3.6+).

Are Python strings mutable?

No — strings are immutable. Every method like .upper() or .replace() returns a brand-new string; the original is untouched.

How do I reverse a string in Python?

Use slice notation with a negative step: text[::-1].

What's the difference between split() and split(',')?

split() with no argument splits on any run of whitespace; split(',') splits on each comma exactly.

🎓 Want to master Python properly?This reference covers one topic — the full free course takes you from zero to projects, with quizzes and a certificate.
Start the Free Python Course →

§Related Python Topics

← Data TypesLists →