How to interpolate data values into strings in Python?

We can interpolate data values into strings using various formats. We can use this to debug code, produce reports, forms, and other outputs. In this topic, we will see three ways of formatting strings and how to interpolate data values into strings.

Python has three ways of formatting strings:

  • % − old school (supported in Python 2 and 3)

  • format() − new style (Python 2.6 and up)

  • f-strings − newest style (Python 3.6 and up)

Old Style: % Formatting

The old style of string formatting has the form format_string % data. The format strings are nothing but interpolation sequences.

Syntax Description
%s string
%d decimal integer
%x hexadecimal integer
%o octal integer
%f decimal float
%e exponential float
%g decimal or exponential float
%% literal %

Basic % Formatting Example

# Basic % formatting with different types
print('String format: %s' % 42)
print('Decimal format: %d' % 42)
print('Hexadecimal format: %x' % 42)
print('Exponential format: %g' % 42)
String format: 42
Decimal format: 42
Hexadecimal format: 2a
Exponential format: 42

Multiple Values with % Formatting

When using multiple values, group them in a tuple. You can also add formatting options between the % and type specifier ?

player = 'Roger Federer'
country = 'Switzerland'
titles = 20

# Multiple values require a tuple
print('Tennis player %s from %s won %d titles' % (player, country, titles))

# Formatting options: width and alignment
print('Player: %s' % player)
print('Player: %20s' % player)  # Right-aligned in 20 chars
print('Player: %-20s' % player) # Left-aligned in 20 chars
print('Player: %.5s' % player)  # Only first 5 characters
Tennis player Roger Federer from Switzerland won 20 titles
Player: Roger Federer
Player:        Roger Federer
Player: Roger Federer       
Player: Roger

New Style: format() Method

The format() method uses {} placeholders and was introduced in Python 2.6. It offers more flexibility than % formatting ?

player = 'Roger Federer'
country = 'Switzerland'
titles = 20

# Basic format() usage
print('Tennis player {} from {} won {} titles'.format(player, country, titles))

# Positional arguments
print('Player {1} from {2} won {0} titles'.format(titles, player, country))

# Named arguments
print('Player {player} from {country} won {titles} titles'.format(
    player='Roger Federer', country='Switzerland', titles=20))

# Alignment options
print('Left: {:<10s} | Center: {:^10s} | Right: {:>10s}'.format('Text', 'Text', 'Text'))
Tennis player Roger Federer from Switzerland won 20 titles
Player Roger Federer from Switzerland won 20 titles
Player Roger Federer from Switzerland won 20 titles
Left: Text       | Center:    Text    | Right:       Text

Latest Style: f-strings

f-strings (formatted string literals) were introduced in Python 3.6 and are now the recommended approach. They are faster and more readable ?

player = 'Roger Federer'
country = 'Switzerland'
titles = 20

# Basic f-string usage
print(f'Tennis player {player} from {country} won {titles} titles')

# Expressions inside f-strings
print(f'{player} needs {25 - titles} more titles to reach 25')

# Formatting and method calls
print(f'Player: {player.upper():^20s} | Titles: {titles:>3d}')

# Multiple lines
message = (f'Tennis legend {player} '
          f'from {country} '
          f'won {titles} Grand Slam titles')
print(message)
Tennis player Roger Federer from Switzerland won 20 titles
Roger Federer needs 5 more titles to reach 25
Player:   ROGER FEDERER    | Titles:  20
Tennis legend Roger Federer from Switzerland won 20 Grand Slam titles

Practical Example: Rankings Report

Here's a real-world example showing why f-strings are preferred for readability ?

players = [
    ('Federer', 20),
    ('Nadal', 22),
    ('Djokovic', 24),
]

print('=== Tennis Rankings ===')

# Using f-strings (recommended)
for rank, (name, titles) in enumerate(players, 1):
    print(f'#{rank}: {name:<10s} = {titles:>2d} titles')

print('\n=== Same output using % formatting ===')

# Using % formatting (older style)
for rank, (name, titles) in enumerate(players, 1):
    print('#%d: %-10s = %2d titles' % (rank, name, titles))
=== Tennis Rankings ===
#1: Federer    = 20 titles
#2: Nadal      = 22 titles
#3: Djokovic   = 24 titles

=== Same output using % formatting ===
#1: Federer    = 20 titles
#2: Nadal      = 22 titles
#3: Djokovic   = 24 titles

Comparison of Methods

Method Python Version Performance Readability Best For
% formatting All versions Good Fair Legacy code
format() 2.6+ Good Good Complex formatting
f-strings 3.6+ Fastest Excellent Modern Python code

Conclusion

f-strings are the recommended approach for string formatting in modern Python due to their speed, readability, and ability to include expressions. Use format() for complex formatting needs, and % formatting only when maintaining legacy code.

Updated on: 2026-03-25T12:15:51+05:30

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements