Is there a Python equivalent to Ruby's string interpolation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Ruby's #{} string interpolation has a direct equivalent in Python: f-strings (formatted string literals), available since Python 3.6. Before f-strings, Python offered str.format() and % formatting. All three approaches embed variables and expressions inside strings, but f-strings are the closest match to Ruby's syntax in terms of readability and conciseness.
Ruby String Interpolation
Ruby's #{} only works inside double-quoted strings ("..."). Single-quoted strings ('...') treat #{} as literal text.
Python f-strings (Best Equivalent)
F-strings are prefixed with f or F and use {} for interpolation. Any valid Python expression works inside the braces.
f-string Features
str.format() (Python 2.6+)
% Formatting (Old Style)
% formatting is the oldest approach and still works, but f-strings and str.format() are preferred for new code.
Template Strings (Safe User Input)
Template is useful when the format string comes from user input because it does not allow arbitrary expression evaluation (unlike f-strings).
Comparison
| Feature | Ruby #{} | Python f-string | str.format() | % formatting |
| Syntax | "#{expr}" | f"{expr}" | "{}.format()" | "%s" % val |
| Expressions | Yes | Yes | Limited | No |
| Min Python version | N/A | 3.6 | 2.6 | All |
| Performance | Fast | Fastest | Slower | Moderate |
| Format specs | Ruby methods | {val:.2f} | {:.2f} | %.2f |
Common Pitfalls
- Using f-strings with untrusted input: f-strings evaluate arbitrary Python expressions. Never use
eval(f"...")or construct f-strings from user-provided templates — this is a code injection risk. UseTemplatefor user-supplied format strings. - Forgetting the
fprefix:"{name}"is a plain string containing the literal text{name}. Onlyf"{name}"interpolates the variable. This is a common mistake when copying code. - Backslashes inside f-string braces: Backslashes are not allowed inside
{}in f-strings. Use a variable:newline = '\n'; f"line1{newline}line2"instead off"line1{'\n'}line2". - Single vs double quotes in Ruby: Ruby only interpolates inside double-quoted strings.
'Hello #{name}'prints the literal#{name}. Python f-strings work with both single and double quotes. - Performance of
str.format()in loops: In tight loops, f-strings are measurably faster thanstr.format()because they are compiled at parse time rather than being method calls at runtime.
Summary
- Python f-strings (
f"{var}") are the direct equivalent of Ruby's#{}interpolation - f-strings support expressions, method calls, format specifiers, and debug output (
=) str.format()and%formatting are older alternatives still used in Python 2 codebases- Use
Templatestrings for user-provided format strings to prevent code injection - f-strings are the fastest string formatting method in Python
- Always include the
fprefix —"{var}"withoutfis a plain string literal
Related reading
- Is there a short contains function for lists?
- Is there a simple way to delete a list element by value?
- Is there a simple way to delete a list element by value?
- Is there a standardized method to swap two variables in Python?
- Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?
- Is there a way to broadcast to all clients except sender in python?
- Is there a way to choose the k nearest neighbors in scikits learn with a user defined distance metric?
- Is there a way to clear Python's IDLE window?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.