How To Print A Variable In Python

Python is a programming language that may be used in a variety of ways. When you print a variable with a string, the contents of the variable are shown within the string. The print statement can be used in a variety of situations. Variables, strings, and other Python data types may all be printed using the print command.

Depending on the programmers’ preferences, the users may utilize the statement in a range of methods. This content will go through how to print a variable in Python using several techniques.

Using The Print Statement

The print statement is supported by all Python versions; however, the syntax differs according to the Python version. The print statement in Python 2 has a very basic syntax. The users only need to utilize the print keyword. To perform the print statement in Python 3 without problems, the print keyword must be preceded by parentheses.

Sample codes:

# PYTHON_2
# TO_PRINT_THE_VARIABLES
print 'HELLO THERE!'

Output

HELLO THERE!

# PYTHON_3
# TO_PRINT_THE_VARIABLES
print ('HELLO THERE!')

Output

HELLO THERE!

Although the print statement in Python 2 may be performed with parenthesis, Python 3 throws an error if the parentheses are not utilized in the statement.

Using The F-String

The f-string is another technique of string formatting that has an advantage over the other processes since it is quicker. Add f in front of a string literal and var within the string literal in a print() statement to print the string literal with the variable var added at the desired position.

Sample code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 3
var2= 'Page'
print(f'Welcome to the {var2} {var1}')

Output

Welcome to the Page 3

Using The Comma ‘,’

In Python, the most basic way for printing variables would be to have a comma to separate them in a print statement.

In Python programming, the following code outputs the variable and string on the same line.

Sample code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 3
var2= 'Page'
print("Welcome to the",var2,var1)

Output

Welcome to the Page 3

Using The String Formatting “%”

String formatting can be used in addition to just printing the statements. This offers the user additional control over the string-like setting, allowing them to modify padding, alignment, establish precision, and sometimes even width.

Using the (%) percent sign is one of two techniques to apply string formatting. A placeholder for the variable is a percent sign (%) followed by a letter. When the user needs to substitute numerical numbers, for example, the user could be able to use (percent) % d as a placeholder.

Sample code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 3
var2= 'Page'
print("Welcome to the {} {}".format(var2,var1))

Output

Welcome to the Page 3

Read More: How to Check if a List Is Empty in Python

Using The + Operator

When utilizing the print statement, the + operator can be used to separate the variables.

Sample code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 3
var2= 'Page'
print('Welcome to the' + var2 + str(var1))

Output

Welcome to the Page 3

An alternative technique that makes use of the + operator:

To convert obj to a string, use str(obj). If obj is already a string, this step is redundant (but helpful). Between the string literals str1 and str2, use print(str1 + var + str2) to output the previous result var.

Sample Code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 3
var1= str(var1)
print('Welcome to the Page' + str(var1)+ ' all ' )

Output

Welcome to the Page 3 all

In the print statement, the user may use the + operator to separate variables, strings, and some other python data.

Using The String Concatenation

String concatenation is a technique for joining strings together. The “+” character is used between two variables or strings to do this. This allows every user to print variable values with the string or variables using Python. This approach does not add spaces; therefore the users would have to manually add them. As a result, this method isn’t utilized very often.

Sample Code:

# PYTHON_PROGRAM
# TO_PRINT_THE_VARIABLES
var1= 'Our'
print("Welcome" +" "+ var1 +" "+ "Page!")

Output

Welcome to Our Page!

Conclusion

Thus, it is demonstrated through a few different ways to print a variable in Python. All the approaches provide the same result, as illustrated above. And the approach to be used is dependent on the individual’s preferences.