How to Print variable and string in Python

In this post, we are going to learn how to Print variable and string in Python. Whenever it comes to learning a new language and at a beginner level first step is to learn to print any string, the variable value on the console. There are multiple ways to print output in python that we will cover with program examples

1. How to Print variable and string in python


To print anything, a message, variable, or string in Python the print() function is used. The syntax to use a print keyword is different based on which version of python we are using it. The print function is consist of print keyword along with opening and closing paratheses ().

In Python2 the print keyword is used with or without parthesises

#python 2 print
print                                    # add a newline charcater    
print ("Hi,how are you!")    
print "welcome to devenum"

In the case of Python3, the print keyword is always used with paratheses to avoid the paratheses will end up with an error

#python 3
print()                                  # add a newline charcater    
print ("Hi,how are you!")    
print "welcome to devenum"

2. How to print in one line python


By default python print() function prints in a new line.The print() function end parameter the value of this parameter is newline character(\n).We can end the newline with a character/string with the help of this parameter. Let us understand how to use end in python

message = "Hi!"
site = "devenum.com"

print(message, end =",")


print("admin",end ="@")

print(site)

Output

Hi!,admin@devenum.com

3. Python Print multiple arguments separated by comma(,)


In this example, we will understand How to Print variable and string in Python separated by a comma. The print() function accepts multiple arguments separated by comma(,) it concatenates all passed arguments and inserted a single comma between them. Let us understand with an example.

message = "Hi! "
site = "devenum.com"
print(message,'welcome to', site)

Output

Hi!  welcome to devenum.com 

The print() function argument sep is stand for separator and the default value is single space (‘ ‘). We can suppress it by passing an empty string. We can choose the separator as per need examples (‘/’,’->’,’-‘).

message = "Hi!"
site = "devenum.com"
print(message,'welcome to', site,sep=None)
print(message,'welcome to', site,sep='')
print(message,'welcome to', site,sep='\n')

Output

Hi! welcome to devenum.com
Hi!welcome todevenum.com
Hi!
welcome to
devenum.com

4. string formatting to print string and variable


The curly brackets { } are used as a placeholder where the value of the variable will be added. To use this inside print statement, there is text with a double quotation and a set of curly braces as a placeholder followed by the string format() method. Let us understand with the below example how to print strings and variables.

message = "Hi!"
site = "devenum.com"

print("{} welcome to {}".format(message,site))

Output

Hi! welcome to devenum.com

5. Python Print variable using f-string


The f-string literal is the latest way to format a string in python. The “f–string literal“ was introduced in python version 3.6. The “f-string” is used for formatting the string that is easy to use and readable. To use it we have to start the string with “F” or “F” before quotation marks and write a python expression or variable that contains literal values between { } that are evaluated at runtime.

Ways to format string before f-string


  • % formatting
  • str.format() method
name = "Max"
message = "Hi!"
site = "devenum.com"  
print( f"{message} {name} Welcome to, {site}")

Output

Hi! Max Welcome to, devenum.com

6. Print variable value using %


In this example, we have shown how to use % string formatting and Its limitation. It does not display the dictionary and tuple correctly.

Python Program Example

name = "Max"
message = "Hi!"
site = "devenum.com"
print("%s %s Welcome to %s"%(message,name,site))

Output

Hi! Max Welcome to devenum.com

7. Python Print string and variable using + operator


We can concatenate the string by using the python plus operator (+) is only used for string-type variables. To concatenate integer data with string the str() function is used.

name = "Max"

site = "devenum.com"


print("Hi there!  " + name + " Welcome to "+site)

print("Hi there! " + name + " Welcome to "+site,str(42))


Output

Hi there!  Max Welcome to devenum.com
Hi there! Max Welcome to devenum.com 42

Summary

In this post, we have learned how to Print variable and string in Python version 3.x and 2.x with examples by using print statements in Python.