Python Print Variable

In this Python tutorial, you will learn about Python print variables with practical examples.

While working on a project, we needed to display some messages on the server. In this type of situation, you can display the message using the print() method and sys.stdout.write() method.

How to Print a Variable in Python

Suppose I want to display a particular message multiple times; then I will not write that message again and again, so we will store it in a variable and display it by calling the variable name.

I found 2 ways to print a variable in Python. Let’s explore both ways and see how to print multiple variables in Python.

Python Print Variable With String using print() Method

First, we will use a print() method, which is the most common way to display any value in Python.

Syntax

print(var_name)
  • print(var_name): This syntax is used to display a value of the variable contains.

There are multiple ways to display the variable value with the print() statement.

Let’s see an example of all the ways of Python Print Variable.

name = "Peter"

message = "Welcome to Python Guides"

# Printing Single variable:

print(message)

# Printing multiple variable

print(message,name)

# Printing variable using formatting

print(f"Hii {name}, {message}")

# Printing variable using format() method

print("Hello {}, {}".format(name, message))
Python Print Variable With String using print() Method

In the above code, we’ve covered all the possible ways to display the variable name using the print() method in Python. First, we print a single variable like this print(message), then we print multiple variables like this: print(message, name), and then we use formatting in Python like this: f”Hii {name}, {message} and “Hello {}, {}”.format(name, message) inside print method.

READ:  Jax Vs PyTorch [Key Differences]

So these are the different ways to print a variable with a string in Python using the print() method.

Python Print String and Variable using sys.stdout.write() Method

We can also print strings and variables using sys.stdout.write() method in Python. Here, it can display only a string value or a variable that contains a string collection as a value.

Before using stdout.write() method; you have to import the sys library in Python.

Syntax

sys.stdout.write("string")
  • sys.stdout.write(“string”): Here, in the place of string, you can give a variable name that contains a string value. If you give any other datatype in this method, then it can give an error like this: TypeError: write() argument must be str, not int.

Let’s understand how we can use this method to print a variable in Python.

import sys

greeting = "Welcome to Python Guides!" 
sys.stdout.write(greeting)
Python Print String and Variable using sys.stdout.write() Method

In the above code, first, we import the sys module, which is a built-in module in Python. Then, we create a variable named greeting and give a Welcome message to the users.

Now, we need to display the message to the users, so we are using the sys.std.write() method like this: sys.stdout.write(greeting). Here, it will check whether the given variable contains strings or not. If yes, then it will print the variable in Python.

Conclusion

In this article, you learned everything about how Python prints variables in two different ways: using the print() method and sys.stdout.write() method.

We’ve given a practical example of printing single variables and multiple variables, then using formatting and the format() method with the print() statement in Python.

You may like to read: