Insert a Variable into a String - Python
Last Updated :
28 Apr, 2025
The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectively.
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.
Python
a = "Python"
res = f"This is {a} programming!"
print(res)
OutputThis 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.
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)
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.
Using + operator
+ operator is a basic method to combine strings and variables, but it can get messy with multiple variables or different data types.
Python
a = "Hello"
b = "World"
res = a + " " + b
print(res)
Explanation: Using the + operator, the variables a and b are joined with a space between them, manually concatenating strings to create dynamic content.
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)
OutputThis 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.
Similar Reads
Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
2 min read
Convert tuple to string in Python The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
2 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Python | Add one string to another The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py
5 min read
String Interning in Python String interning is a memory optimization technique used in Python to enhance the efficiency of string handling. In Python, strings are immutable, meaning their values cannot be changed after creation. String interning, or interning strings, involves reusing existing string objects rather than creat
2 min read