Home »
Python »
Python programs
How to print spaces in Python?
Python | Printing spaces: Here, we are going to learn how to print a space/ multiple spaces in the Python programming language?
Submitted by IncludeHelp, on April 25, 2020
While writing the code, sometimes we need to print the space. For example, print space between the message and the values, print space between two values, etc. In the Python programming language, it's easy to print the space.
Following are the examples demonstrating how to print the spaces in Python?
Example 1: A simple way to print spaces
The simple way is to give the space between the message wherever we need to print space in the output.
print(' ')
print(" ")
print("Hello world!")
print("Hello world")
Output
Hello world!
Hello world
Example 2: Printing spaces between two values while printing in a single print statement
When we print multiple values separated by the commands using the print statement – Python default print a between them.
x = 10
y = 20
print("x:",x)
print("y:",y)
Output
x: 10
y: 20
Example 3: Give multiple spaces between two values
To give multiple spaces between two values, we can assign space in a variable, and using the variable by multiplying the value with the required spaces. For example, there is a variable space assigned with space and we need to print 5 spaces - we can use space*5 and it will print the 5 spaces.
x = 10
y = 20
space = ' '
'''
2 spaces would be here
1 space after first parameter,
1 space by using space variable
1 space after second parameter
'''
print("Hello",space,"world")
'''
7 spaces would be here
1 space after first parameter,
5 space by using space*5
1 space after second parameter
'''
print("Hello",space*5,"world")
'''
12 spaces would be here
1 space after first parameter,
10 space by using space*10
1 space after second parameter
'''
print("Hello",space*10,"world")
# for better better understanding
# assign space by any other character
# Here, I am assigning '#'
space = '#'
print("Hello",space,"world")
print("Hello",space*5,"world")
print("Hello",space*10,"world")
# printing values of x and y with spaces
space = ' '
print("x:",space*10,x)
print("y:",space*10,y)
Output
Hello world
Hello world
Hello world
Hello # world
Hello ##### world
Hello ########## world
x: 10
y: 20
Python Basic Programs »
ADVERTISEMENT
ADVERTISEMENT