Interview Q&A · Python · 30 questions in this topic

Strings, Lists, Tuples, Sets & Dictionaries — Python Interview Q&A

All Python topics

The built-in containers and the questions that follow them everywhere: mutation, hashing, copying, comprehensions and the cost of each choice. Part of Strings, Lists, Tuples, Sets & Dictionaries · 375 questions in the Python bank.

What are strings in Python, and how are they represented internally?

Answer

A string (str) is an immutable sequence of Unicode characters. Internally, CPython stores strings as arrays of fixed-width code points, choosing the smallest representation (1, 2, or 4 bytes per character) that fits the string's content — an optimization called flexible string representation.

Since Python 3.3 (PEP 393), CPython no longer uses one-size-fits-all UTF-32 storage. A string containing only ASCII/Latin-1 characters uses 1 byte per character; one containing characters up to U+FFFF uses 2 bytes; anything beyond that (emoji, rare scripts) uses 4 bytes.

This is invisible to the programmer — you always just get correct Unicode behavior — but it matters when discussing memory efficiency of large text datasets.

Example

Example.pypython
s = "hello"
print(type(s)) # <class 'str'>
print(len(s)) # 5
print(s[0], s[-1]) # 'h' 'o' -- indexing from front and back

💡 Mention immutability up front — it's the property that explains why every "modifying" string method (.upper(),.replace(), etc.) returns a new string instead of changing the original.

What is string slicing, and how does it work?

Answer

Slicing extracts a sub-sequence from a string (or any sequence) using sequence[start:stop:step]. start is inclusive, stop is exclusive, and step controls direction and stride; all three are optional.

Omitting start defaults to the beginning, omitting stop defaults to the end, and omitting step defaults to 1. Negative indices count from the end (-1 is the last character), and a negative step reverses direction — which is how the famous s[::-1] reversal idiom works. Slicing never raises an IndexError for out-of-range bounds; it just clips to what's available.

Example

Example.pypython
s = "Interview"
print(s[0:4]) # 'Inte'
print(s[:4]) # 'Inte' (same as above)
print(s[4:]) # 'rview'
print(s[-4:]) # 'rview' (last 4 characters)
print(s[::2]) # 'Itriw' (every 2nd character)
print(s[::-1]) # 'weivretnI' (reversed)
print(s[100:200]) # '' (out of range -> empty, no error)

💡 If asked to reverse a string live, s[::-1] is the expected idiomatic answer — but be ready to also explain the manual approach (loop or reversed(s) + "".join(.)) if the interviewer wants to see you reason about the underlying mechanics.

Why are strings immutable in Python?

Answer

Strings are immutable for performance, safety, and hashability: it lets Python cache and reuse (intern) identical strings, safely share them across threads without locking, and use strings as dictionary keys or set members.

If strings were mutable, using one as a dictionary key would be dangerous — modifying the string after insertion would change its hash, corrupting the dictionary's internal structure. Immutability also enables string interning: CPython automatically reuses the same object in memory for many identical short strings (especially identifier-like strings), reducing memory use.

Every operation that looks like it "modifies" a string —.upper(), +=,.replace() — actually builds and returns a brand-new string object.

Example

Example.pypython
s = "hello"
new_s = s.replace("h", "j")
print(s) # 'hello' -- original untouched
print(new_s) # 'jello' -- a new object
a = "python"
b = "python"
print(a is b) # True in most cases -- interning reuses the same object

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits