Goal

You’ll be able to use core string methods confidently, and understand the important, easy-to-forget fact that strings in Python are immutable.

Learn

Strings have many useful built-in methods for common text operations:

name = "  Ahmed  "
name.strip()       # "Ahmed" — removes leading/trailing whitespace
name.upper()       # "  AHMED  "
name.lower()       # "  ahmed  "
name.replace("A", "O")  # "  Ohmed  "
"hello world".split(" ")  # ["hello", "world"]
", ".join(["a", "b", "c"])  # "a, b, c"

Strings can also be combined and formatted. F-strings (formatted string literals) are the modern, readable way to insert variables directly into text:

name = "Ahmed"
age = 25
message = f"My name is {name} and I am {age} years old."

The f before the opening quote enables this — anything inside curly braces gets evaluated and inserted directly into the string.

Here’s a genuinely important detail: strings in Python are immutable — once created, a string’s actual content can never be changed in place. Every string method that appears to “modify” a string, like .upper(), actually returns a brand new string, leaving the original completely unchanged:

text = "hello"
text.upper()
print(text)  # still prints "hello" — the original was never changed!
text = text.upper()  # this reassigns text to the NEW string
print(text)  # now prints "HELLO"

Decision Task

You write name = "ahmed" then name.upper() on its own line, expecting name to now hold “AHMED”. Before reading on: does this actually work, and if not, why not?

Show Answer

No, it doesn’t work as expected. .upper() returns a new string but doesn’t modify the original in place, since strings are immutable in Python. To actually change what name holds, you need to reassign it: name = name.upper(). Calling name.upper() alone just creates and discards a new string, leaving the original name variable completely unchanged.

Common Mistake

Calling a string method expecting it to modify the original variable in place, without reassigning the result back to a variable. Since strings are immutable, this is a genuinely common source of confusing bugs for beginners coming from languages where string modification in place is normal — the fix is always to explicitly reassign: text = text.method(), not just text.method() alone.

Practice Questions

1. Write an f-string that inserts a variable called city into the sentence “I live in [city].”

Show Answer

f"I live in {city}."

2. What does “Hello World”.split(” “) return?

Show Answer

["Hello", "World"] — a list of substrings split on the space character.

3. Why does calling text.strip() alone, without reassigning it, not actually change the text variable?

Show Answer

Because strings are immutable — strip() returns a new string with whitespace removed, but doesn’t modify the original in place; you’d need text = text.strip() to actually update the variable.

4. True or False: “, “.join([“a”, “b”, “c”]) modifies the original list, adding commas between its elements in place.

Show Answer

False — join() returns a brand new string; it doesn’t modify the original list at all.

5. What does .replace(“A”, “O”) do to a string?

Show Answer

Returns a new string with every occurrence of “A” replaced with “O”, leaving the original string unchanged.

Try It Yourself

Without looking back, write code that takes a variable email = ” USER@EXAMPLE.COM ” and produces a cleaned-up version: trimmed of whitespace and converted to lowercase, correctly reassigned to actually update the variable.

Show Answer

email = email.strip().lower() — methods can be chained directly, and the result must be reassigned back to email since strings are immutable and neither method modifies the original in place.

Quick Check

1. What does .strip() do to a string?

Show Answer

Removes leading and trailing whitespace.

2. What does an f-string let you do?

Show Answer

Insert variables directly into a string using curly braces, prefixed with f before the opening quote.

3. Are strings in Python mutable or immutable?

Show Answer

Immutable — their content can never be changed in place.

4. If strings are immutable, how do you actually “update” a variable holding a string?

Show Answer

By reassigning the variable to the new string returned by a method, e.g. text = text.upper().

5. What does .split(” “) return?

Show Answer

A list of substrings, split wherever the space character occurs.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.