python

Python tip: How to insert a variable value inside a string

Python is a programming language that is widely used in many different applications today. Python is known for its ease of use and its readability, which makes it a great choice for beginners. One of the great things about Python is that you can insert a variable value inside a string, which can be very useful when you want to print out a message that includes a dynamic value.

Solution 1: Using the f-string format

We've all been there before: we need to insert a variable value inside a string. Luckily, Python has a handy tool called f-strings that make this process easy.

In this Python tip, we'll show you how to use f-strings to insert a variable value inside a string.

prog_txt = 'programming'

result = f'Python is a {prog_txt} language'

print(result)

Output

Python is a programming language

This is an example of string formatting. The f-string uses Python's interpolation syntax to insert the value of the prog_txt variable into the string. The result is that the string "Python is a programming language" is printed to the console.

Solution 2: Using '+' operator

Here we will explain how to insert a variable value inside a string using the '+' operator. This can be useful when you want to dynamically generate a string based on the value of a variable.

Place the variable name between the + operators used inside a string and you can use that variable value inside the string. To understand it check the below code example:

prog_txt = 'programming'

result = 'Python is a '+ prog_txt +' language'

print(result)

Output

Python is a programming language

This code is concatenating the string "Python is a " with the string variable prog_txt and storing the result in the variable result. The value of the result is then printed to the console.

Solution 3: Using String.format() function

If you want to insert a variable value inside a string, you can use the String.format() function. This function allows you to specify the position of the variable value inside the string, as well as the format of the variable value.

Code example 1: Insert one variable in a string

name = "John Deo"

result = "My name is {}".format(name)

print(result)

Output

My name is John Deo

Code example 2: Insert multiple variables inside a string

first_name = "John"
last_name = "Deo"

result = "My name is {} {}".format(first_name, last_name)

print(result)

Or you can also write the above code as below

first_name = "John"
last_name = "Deo"

result = "My name is {txt1} {txt2}".format(txt1=first_name, txt2=last_name)

print(result)

Output

My name is John Deo

The code example assigns the string values of "John" and "Deo" to the variables first_name and last_name, respectively. The string "My name is {txt1} {txt2}" is then assigned to the variable result, where the placeholder values {txt1} and {txt2} are replaced with the values of first_name and last_name, respectively. Finally, the value of the result is printed on the console.

Code example 3: Insert from a List variable

fruits = ["Apple", "Banana", "Orange"]

result = "Fruits are: {0}, {1}, {2}".format(*fruits)

print(result)

Output

Fruits are: Apple, Banana, Orange

The example is using the format method to print a string that contains the elements from the list named fruits. The * is used to unpack the list so that each element is passed as a separate argument to the format method.

Solution 4: Using % operator

To insert a variable value inside a string using % operator, we use the % symbol followed by the variable name.

For example, if we have a string: 'My name is %s' and a variable: name = 'John', we can use the % operator to insert the value of the variable into the string like this: 'My name is %s' % name.

fruit_name = 'Apples'
num = 10

result = 'We have %d %s' % (num, fruit_name)

print(result)

Output

We have 10 Apples

The python code above is an example of string formatting.

The %d is a placeholder for a number, and the %s is a placeholder for a string.

The values in the parentheses after the % symbols are the values that will be inserted into the placeholders.

In this case, the value of num will be inserted into the %d placeholder, and the value of fruit_name will be inserted into the %s placeholder.

The result of the code will be the string 'We have 10 Apples'.

Was this helpful?