Python Print Variable | Explained

In Python, we need to print the results and analyze the outcomes of that results. In Python, we have the advantage that if we don’t enter the data type, it will automatically detect and print the result. Python also easily prints the variable’s value and does not need any complex statement or function.

In this post, we will elaborate on the possible ways to print variables in Python. This post comprises a set of examples to better convey the concept of printing variables in Python.

Different Ways to Print Variables in Python

To print the variables in python, we used different methods with the print() function. A print() function prints the result of code on the screen by taking input in its parameter. The print() function parameters are any object, variable, or string.

Example 1: Print() Function with Variables

print() function has the simplest syntax in python. It will print the value written between single double or triple quotes. In Python, it is not compulsory to define the data type with input value because python language will automatically detect the data type and show the output. Let’s understand this by a simple example.

Code

#printing the variables with different data types
x_1='Hello and Good Morning'
x_2=123
x_3=0.225
print(x_1)
print(x_2)
print(x_3)

In the above code:

  • We initialize three different variables named “x_1”, “x_2” and “x_3”. All three variables we are using are of different data types i.e., “x_1” is a string and “x_2” is an integer value, and “x_3” is a float value.
  • We print different data type variables by calling their name in print() function parenthesis.

Output

Three different data type values are printed in the above output.

Example 2: Print() Function with More than One Variables

Using the print() function, we can initialize multiple variables in a single line. We make this possible by using commas between different variables. We can pass any value to the variables such as integer, string, floating point, etc. let’s see an example to understand:

Code

#print variables using multiple variable
name, gender, age = "Alexander", 'Male', 48.0
print(name)
print(gender)
print(age)

In the above code:

  • First, we are initializing 3 variables in a single line named “name”,” gender” and “age”.
  • Secondly, we only need to print the variables by calling their variable name in the print function.

Output

The output shows the value of variables called by the print() function.

Example 3: Print() Variable Using the f-string Method

In this method, we put the character f after the opening bracket of the print() function and before starting the quotation sign. This method will help us to call the variables in between the quotation of the print() function. We call the variables by using their names enclosed in curly braces{}. Let’s practice this with an example:

Code

#printing the variables using f-string
x= 'Good'
y= 'Morning'
print(f'Hello and {x} {y}')

In the above code:

  • We initialized two variables named “x” and “y”.
  • Secondly, we use the f string before the starting of the quotation and call the variable {x} and {y} inside the print statement.

Output

The output shows the combined string value  “Hello” with variable values of x “Good” and y “Morning”.

Example 4: Print() Using String Concatenation

We use string concatenation when we want to add variables to the string list. String list contains the sequence of elements having string data type. This method is helpful because we can add the variable value anywhere using the “+” operator with the variable name. Let’s see an example to understand the concatenation method:

Code

#using string concatenation
z1 = "welcome"

#Adding the variable z1 into a string
print("Hello" +" and "+ z1 +" "+ "Guys!")

In the above code:

  • We first take a variable named “z1”.
  • Lastly, we print the variable  “z1” using the + operator after the string value name “and”. We can add different variables by using this method.

Output

The above output shows the result of the print() function using the concatenation of variables into strings.

Example 5: Print-Multiple Variables Using a Comma (,)

This is the simplest method among all of the others to print the variable. We used comma (,) inside the parentheses of the print() function to separate the piece of text from the variable value. We separate two different variable values or separate a variable value with a string using a comma in the print() function bracket.

Code

#using comma to separate and placed in between variables
x1= 'Good'
y1= 'Morning'
print("Hello and",x1,y1)

In the above code:

  • We initialize two different string names as “x1” and “x2”.
  • In the print function, we print “x1” and “y1” variables with the addition of string using the comma technique.

Output

The output shows the value of a string with different variable values separated by a comma.

Example 6: Print() Variable using Format Command

The “format()” method is used for printing different variables inside the string. It is placed between the print statement using curly braces{} in the position where we want to print the variable. We do not put any value between the curly braces “{}”. These empty braces pick the value from the variables used in the “.format()” method. For instance, the “a” and “b” in the “.format(a,b)” method refers to the first and second curly brackets, respectively.  Let’s understand its working via the following example code:

Code

#printing the variables using format code
#by using {} method
x1= 'Good'
y1= 'Morning'
print("Hello and {} {}".format(x1,y1))

In the above code

  • We first initialize two string variables named “x1” and “x2”.
  • Then we place the bracket {} where we want to print our variable value. Here we are placing two brackets {} inside the string of the print function.

Output

In the above output, we printed two variables inside the string using the format() function.

Conclusion

In Python, you can “print variables” using different techniques like the “f-string method”,” string concatenation”, and “.format() function” and also with the simple variable method. Python is a very versatile language where you can print() variables easily using any of these methods. If you want to print multiple variables inside the string, then you may use the format() function technique, and if you need to add a different list with a string, then you can use the concatenation method. This post explained various methods to print variables in Python.