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.
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.
Since Python 3.6, f-strings are the way to build text. Put any expression inside {}.
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'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!
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")) # occurrencescsv = "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)) # 4name = "ada" # name[0] = "A" # TypeError! strings can't be edited in place name = name.capitalize() # make a NEW string instead print(name) # Ada
s.upper() alone does nothing visible — assign it: s = s.upper()"Python"[0:3] is 'Pyt', not 'Pyth'"age: " + 12 is a TypeError — use f-strings or str(12)A string literal prefixed with f that evaluates expressions inside {} — f"Hello {name}" — the modern, fastest way to format text (Python 3.6+).
No — strings are immutable. Every method like .upper() or .replace() returns a brand-new string; the original is untouched.
Use slice notation with a negative step: text[::-1].
split() with no argument splits on any run of whitespace; split(',') splits on each comma exactly.