As a Python developer, I got a requirement to insert variables into strings. There are several methods to achieve this, In this tutorial, I will explain how to insert a Python variable into a string using various methods with examples.
To insert a Python variable into a string using the f-string method, simply prefix your string with an f or F and place the variable inside curly braces {} within the string. For example, if you have a variable name = "John", you can use print(f"My name is {name}.") to include the variable in the output.
There are various methods to put a Python variable into a string. Let me explain each method with examples.
1. Using the Comma Method
The comma method is easy to understand and use. It involves separating the string and the variable with a comma in the print function.
Syntax:
Here is the syntax:
print("string", variable)Example:
Let me show you an example. Below is the Python code.
name = "John"
age = 28
print("My name is", name, "and I am", age, "years old.")Output:
My name is John and I am 28 years old.Here is the output you can see in the screenshot below:

Check out Split a String into Equal Parts in Python
2. Using the Modulus Operator (%)
The modulus operator is an older method but is still widely used. It allows you to insert variables into a string by using % followed by a tuple of variables in Python.
Syntax:
Here is the syntax:
"string %s" % variableExample:
Let me show you an example.
city = "New York"
temperature = 75
print("The temperature in %s is %d degrees Fahrenheit." % (city, temperature))Output:
The temperature in New York is 75 degrees Fahrenheit.Here is the exact output in the screenshot below:

Read Split a String by Index in Python
3. Using the format() Method
The format() method in Python allows you to insert variables into strings using curly braces {} as placeholders.
Syntax:
Here is the syntax:
"string {}".format(variable)Example:
Now, let me show you an example.
first_name = "Alice"
last_name = "Johnson"
print("Hello, my name is {} {}.".format(first_name, last_name))Output:
Hello, my name is Alice Johnson.Here is the output in the screenshot below:

Check out How to Compare Strings in Python?
4. Using F-Strings (Formatted String Literals)
F-strings are the most recent and recommended method for inserting variables into strings in Python. They are concise and easy to read, introduced in Python 3.6.
Syntax:
Here is an example.
f"string {variable}"Example:
Let me show you an example.
state = "California"
population = 39500000
print(f"The population of {state} is approximately {population} people.")Output:
The population of California is approximately 39500000 people.Here is the output in the screenshot below:

Check out Convert String to List in Python Without Using Split
5. Using String Concatenation
String concatenation involves using the + operator to combine strings and variables in Python. This method requires converting non-string variables to strings using the str() function.
Syntax:
Here is the syntax:
"string" + str(variable)Example:
Here is an example.
event = "concert"
tickets = 150
print("We sold " + str(tickets) + " tickets for the " + event + ".")Output:
We sold 150 tickets for the concert.Here is the exact output in the screenshot below:

Conclusion
In this tutorial, I explained, how to insert variables into strings in Python programming using various methods—comma, modulus operator, format(), f-strings, and string concatenation etc. If you still have questions, feel free to comment below.
You may also like:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.