Interview Q&A / Python

BeginnerQuick answer1 min readUpdated 3 Oct 2026

Are strings mutable in Python, and how do you change a character in one?

SHORT ANSWER

Strings are immutable, so s[0] = "x" raises TypeError. To change one, build a new string using slicing, replace(), or join over a list of characters.

Explain it simply

A string is like a sentence printed in a textbook. You cannot rub out one letter and write another; you have to print a fresh copy with the change. Methods such as upper() and replace() never edit the original, they return a new string.

Immutability is also why strings can be dictionary keys and why Python can safely reuse them behind the scenes.

Example

python
s = "cat"
try:
    s[0] = "b"
except TypeError:
    print("cannot assign")      # Output: cannot assign

t = "b" + s[1:]
print(t)                        # Output: bat
print(s)                        # Output: cat
print(s.replace("c", "r"))      # Output: rat

chars = list("cat")
chars[0] = "h"
print("".join(chars))           # Output: hat

What to say in the interview

"Strings are immutable. Any operation that looks like a change returns a new string, and the original is untouched. To edit characters I use slicing and concatenation, replace(), or convert to a list, modify and join back. Immutability makes strings hashable and thread-safe to share."

Common mistakes

  • Calling s.upper() and ignoring the result, expecting s to change.

#python#strings

Related questions

Practise more Python questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Python quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.