4 Ways to Print a Variable Inside String in Python

0
73
4 Ways to Print a Variable Inside String in Python

Printing in Python is important because it will help you debug your program, and you can print all the variables at the different levels of your program to see what is going on. In this article, we will see 4 different ways to print a variable inside a string.

How to print a string in Python?

To print a string in the Python console, use the print() function. The print() function takes any text as a string or object. Then, that object will be converted into a string and printed.

The print() function accepts any number of arguments and prints them out on one line of text.

Program

print("Sorry to say that but cryptos are collapsing!")

Output

Sorry to say that but cryptos are collapsing!

How to print a variable in Python?

To print a variable in Python, use the print() function and pass the variable to it. The print keyword is followed by opening and closing parentheses ().

exchange = "FTX"

print(exchange)

Output

FTX

Python print variable inside a string

To print a variable inside a string in Python, use the format() function inside the print() function. The format() function formats the provided value and adds them inside the placeholder of a string. The print() is a built-in function that prints the defined text to the console.

exchange = "FTX"

print("Sorry to say that {} has filed for bankruptcy!".format(exchange))

Output

Sorry to say that FTX has filed for bankruptcy!

In this example, we are printing a variable exchange inside a string “Sorry to say that {} has filed for bankruptcy!”.

You can use the string formatting by including a set of opening and closing curly braces, {}, where you need to insert the variable’s value.

Let’s see another approach.

Using the f-strings to print a variable inside a string

The f-string is a way of formatting strings using embedded Python expressions inside string constants. The f-string is new to Python 3. Python Formatted string literals are parser features that convert f-strings into a series of string constants and expressions. In the end, they all join together to build the final string, which we can print in the console.

Implementation

exchange = "FTX"

print(f"Sorry to say that {exchange} has filed for bankruptcy!")

Output

Sorry to say that FTX has filed bankruptcy!

The f-string is a cleaner approach to printing a variable’s value inside a string.

Using concatenation to print a variable inside a string

The concatenate operator (+) helps us put a variable inside a print() function and prints it in the console.

One thing to keep in mind is that the concatenation approach can only be used for strings, so if the variable you need to concatenate with the rest of the strings is an integer, convert it first from an integer to a string using the str() function.

Programming implementation

exchange = "FTX"

print("Sorry to say that " + exchange + " has filed for bankruptcy!")

Output

Sorry to say that FTX has filed for bankruptcy!

You can see that we put the “exchange” variable inside the print() function and print its value in the console.

Printing a variable inside a string using the % sign

Use the %s format specifier to tell Python where to substitute the variable’s value represented as a string. The % operator is the old style to format a string in Python.

Programmatic implementation

exchange = "FTX"

print("Sorry to say that %s has filed for bankruptcy!" %exchange)

Output

Sorry to say that FTX has filed for bankruptcy!

You can see in the output that the value of the variable “exchange” is printed successfully.

How to print multiple variables inside a string in Python

To print multiple variables inside a string, use the f-string in Python. The f-string can helps us print the value of multiple variables in the console.

exchange = "FTX"
culprit = "Sam bankman fried"

print(f"Due to {culprit}, {exchange} has filed for bankruptcy!")

Output

Due to Sam bankman fried, FTX has filed for bankruptcy!

We printed two variables: “culprit” and “exchange”, using the f-strings. You can achieve the same using the string.format() method or concatenate operator(+).

Conclusion

The best way to print a variable inside a string is to use either format() function or f-string because they are a cleaner and more understandable approach.

Further reading

Print no newline in Python

Python print line break

How to print bold text

Leave A Reply

Please enter your comment!
Please enter your name here

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