How to put a variable inside a string in Python?

In this article, we will learn to put variable’s value inside a string using Python Programming language. This can be very helpful where a statement is same but some value we get from an any other source like API changes daily. In that scenario we need to figure out a way to put the value of that variable in the statement. Let’s see some of these methods in Python.

Table Of Contents

Put a variable inside a string using f-string

The first method we will be using is the f-string method. Also known as the Literal String Interpolation. To create a f-string you need to add f as a prefix to string and curly braces in the place of variable. It can be used with multiple variables.

EXAMPLE :

temp = 45

# f prefix denotes the f-string and variable temp 
# is enclosed with curly braces.
statement = f"Today's maximum temperature is expected to be {temp} degrees Celsius"

print(statement)

OUTPUT :

Advertisements
Today's maximum temperature is expected to be 45 degrees Celsius

In the code and output above, you can see how easy and helpful is the f-string method, to put a variable’s value inside a string. Just add f as prefix before the double quotes and enclose variable in the curly braces.

Put a variable inside a string using format() method

Next method that we will be using is the format() method. It is the built-in string class method, which provides formatting options of the strings in Python. Just insert curly braces in the string and provide the variable in format() method as a parameter.

SYNTAX :

str.format(value)

It recives only one parameter which is the str/value which needs to formatted. See an example below :

EXAMPLE :

temp = 45

# provide curly braces where you need the variable's value.
statement = "Today's maximum temperature is expected to be {} degrees Celsius"

# provide variable as an argument in the format() method
strValue = statement.format(temp)

print(strValue)

OUTPUT :

Today's maximum temperature is expected to be 45 degrees Celsius

In the code and output above, you can see that by using format() method we have successfully inserted the value of variable temp in the statement. Just put a pair of curly braces at the place where you need to put the variable’s value. You can also add multiple variable inside a single string. Just pass all the variables as argument sequence wise, like the value needed in the first curly braces should be in first place as an argument.

Put a variable inside a string using String Concatenation

Next we will be using the string concatenation to put a variable’s value inside a string. String Concatenation is adding strings using + operator. Here we will be adding our variable inside the string using the + operator. This method has one drawback, it only concatenates strings not int or any other data type. If you need any int value as a variable’s value, first you need to convert it to string using the str() method otherwise it will throw a TypeError. Let’s see an Example :

EXAMPLE :

temp = 45

# + operator is used at our desired place where we need to 
# put our variable's value also int has been converted to 
# str using str() function.
statement = "Today's maximum temperature is expected to be " + str(temp) + " degrees Celsius"

print(statement)

OUTPUT :

Today's maximum temperature is expected to be 45 degrees Celsius

So in the code and output above you can see the use of string concatenation and by using it we put variable temp’s value inside the string. We have also used str() method to convert int to str, since it accepts only string.

Put a variable inside a string using % Operator

Next we will be using the % Operator to put a variable’s value inside a string. This is very old method and used in other programming language like c. The %s is used for string and %d is used for integer. Let’s see an Example :

EXAMPLE :

temp = 45

# Since we are using an integer so we will use %d 
# at the place where we need our variable's value.
statement = "Today's maximum temperature is expected to be %d degrees Celsius" % temp

print(statement)

OUTPUT :

Today's maximum temperature is expected to be 45 degrees Celsius

In the code and output above you can see we have successfully put the temp variable’s value inside the statement variable by using % operator. We have used %d for integer and also need to denote the variable at the end like here temp has been denoted with % operator.

Summary

In this article we learned to put a variable’s value inside a string using the Python programming language. We learned about four different methods.
Most useful method is format() method as it is easy to understand and you just need to pass value as an argument and denote curly braces at the place of variable. In other methods like method 3 and method 4 are smaller but a mistake can lead you to an error. Make sure to learn each and every method and always run these codes in your machines. We have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top