Insert a Variable into a String - Python

Last Updated : 29 Jul, 2026

Python provides several ways to insert variables into strings, allowing dynamic text generation for messages, formatted output, file names, and user input. Common approaches include f-strings, the format() method, string concatenation, and % formatting.

Example:

Python
name = "Alice"
print(f"Hello, {name}!")

Output
Hello, Alice!

Using f-strings

f-strings (formatted string literals) were introduced in Python 3.6 and have quickly become the most recommended way to insert variables into strings. They are concise, readable and faster than other traditional methods.

Example:

Python
a = "Python"
res = f"This is {a} programming!"
print(res)

Output
This is Python programming!

Explanation: In an f-string, the variable a is inserted directly by placing it inside {} within a string prefixed with f, enabling dynamic content without manual concatenation.

Example 2: Formatting Expressions with f-strings

Python
name = "Alice"
age = 20
print(f"{name} will be {age + 1} next year.")

Output
Alice will be 21 next year.

Explanation: Variables and expressions can both be placed inside {} and "age + 1" is evaluated before being inserted into the string.

Using format()

Example 1: Before f-strings, the .format() method was the standard way to insert variables into strings. It remains widely used, especially in projects requiring compatibility with Python versions earlier than 3.6.

Python
a = "Hello"
b = "world"

res = "{} {}".format(a, b)
print(res)

Output
Hello world

Explanation: In the format() method, placeholders {} are used inside the string, and variables a and b are inserted by passing them to format(), allowing dynamic content without manual concatenation.

Example 2: Add named placeholders

Python
name = "Alice"
age = 20
print("Name: {name}, Age: {age}".format(name=name, age=age))

Output
Name: Alice, Age: 20

Using + operator

The + operator concatenates multiple strings. It is suitable for simple cases but becomes less readable when combining many variables or different data types.

Python
a = "Hello"
b = "World"

res = a + " " + b
print(res)

Output
Hello World

Explanation: Using the + operator, the variables a and b are joined with a space between them, manually concatenating strings to create dynamic content.

Note: All operands must be strings. Non-string values must be converted using str().

Using % formatting

Example 1: This method was used in earlier versions of Python and is still supported, but it's less preferred today due to better alternatives like format() and f-strings.

Python
a = "Python"
res = "This is %s programming!" % a
print(res)

Output
This is Python programming!

Explanation: In % formatting, the %s placeholder inside the string is replaced by the value of a, allowing variable insertion in a style similar to old C-style formatting.

Example 2: Formatting Numeric Values with %

Python
score = 95
percentage = 92.75

print("Score: %d" % score)
print("Percentage: %.2f%%" % percentage)

Output
Score: 95
Percentage: 92.75%

Explanation:

  • %d inserts an integer value into the string.
  • %.2f inserts a floating-point number with 2 decimal places.
  • %% prints a literal % character because % is used as a formatting operator.

Note: % formatting supports various format specifiers such as %s for strings, %d for integers, and %f for floating-point numbers. However, f-strings and the format() method are generally preferred for new Python code due to their improved readability and flexibility.

Comment