What is string formatting in Python?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Python development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Python offers multiple string formatting approaches. f-strings (Python 3.6+, preferred): f"Hello, {name}! Age: {age + 1}" — fast, readable, and support any expression inside braces. str.format(): "Hello, {}! Age: {age}".format(name, age=25). %-formatting (old style, avoid): "Hello, %s! Age: %d" % (name, age). F-strings also support format specifications: f"{value:.2f}" (2 decimal places), f"{number:,}" (thousands separator), f"{name!r}" (repr), f"{value:>10}" (right-align in 10 chars). Multi-line f-strings work normally inside triple quotes. Nested f-strings: f"{\"yes\" if condition else \"no\"}".
Common Mistake
A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.
Previous
What is exception handling in Python?
Next
What is Python's with statement (context manager)?