Print variable in python using 4 different methods

In this tutorial you will learn about how you can declare and print variables in python using multiple methods. Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data. The handling of variable is different with Python so if you wish to print text with variable then you need to use proper syntax with print () function.

 

Python version in my environment

# python3 --version
Python 3.6.8

 

How to print variable in python

If you only want to print the variable having integer value without any other content

var = 10

To print this variable use:

# print(var)

The output would be:

10

Similarly to print a variable having string, without any other content

var = "deepak"

To print this variable use the same method:

# print(var)

The output would be:

deepak

 

How to print variable and string in same line in python

As we know Python follows and what we call as Object Model so every number, string, data structure, function, class, module, and so on is considered as an Object. Now assuming you want to use these variables in some text statement such as "My name is <VAR>" where VAR should be replaced with the Variable. To achieve this I will share various methods:

I will use these variables across all the examples from different methods:

var1 = 2
var2 = 'deepak'
var3 = 'amit'

The end goal is to have

I have 2 students: deepak and amit

 

Method 1: Using comma , character

  • Use commas , to separate strings and variables while using print function in python
  • Just make sure that you define your content within double or single quotes " " or ' ' while the variables should be separated by comma , character
print("I have", var1, "students:", var2, "and", var3)

Output:

I have 2 students: deepak and amit

 

Method 2: Using Format String

  • Every time you put " (double-quotes) around a piece of text you have been making a string
  • You embed variables inside a string by using a special {} sequence and then put the variable you want inside the {} characters.
  • You also must start the string with the letter f for “format,” as in f"Hello {somevar}".
  • This little f before the " (double-quote) and the {} characters tell Python 3, "Hey, this string needs to be formatted. Put these variables in there."
print(f"I have {var1} students: {var2} and {var3}")

Output:

I have 2 students: deepak and amit

 

Method 3: Using Format String with Positional Argument

  • Format Specifications can also be used as replacement fields contained within a format string to define how individual values are presented
  • Starting with python version 3.1 the positional argument specifiers can be omitted for str.format(), so '{} {}'.format(a, b) is equivalent to '{0} {1}'.format(a, b).
print("I have {} students: {} and {}".format(var1,var2,var3))

To access arguments using position

print("I have {0} students: {1} and {2}".format(var1,var2,var3))

You can change the positional argument sequence and accordingly it would take the values from str.format()

print("I have {2} students: {1} and {0}".format(var3,var2,var1))

Output:

I have 2 students: deepak and amit

 

Method 4: Using plus + character

  • Ideally + character is used for mathematical operations but you can also use + to join to variables
  • Additionally we would need str() function to convert the input value to string to be able to print variables using this method
  • Because by default + would consider var1 as integer here and fail to concatenate since var2 and var3 are strings
print("I have " + str(var1) + " students " + var2 + " and " + var3)

Output:

I have 2 students: deepak and amit

 

I have combined all these methods in the same script:

Print variables and strings in python using 4 different methods
Python print variable

The output from this script:

Print variables and strings in python using 4 different methods
Python script output

 

 

Conclusion

In this tutorial we learned how to print variables with python using different possible methods. You can choose the preferred method based on your requirement. Python programming language defines variable differently compared to other language such as bash and perl. In shell script and perl we call a variable by appending dollar sign $ to the variable name but that is not the case with python.

Lastly I hope the steps from the article to declare and print variable in Python was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
Common String Operations
What's New in Python 3.0

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.