How to give space in Python
In this post, you will learn how to print single or multiple spaces using the Python programming language. Spacing is important in every programming language. This is quite simpler in Python than in other programming languages. Here, we have mentioned different ways to give space in Python.
Simple way to print space in Python
Here is the simple way to print a single space in Python.
# Python program to print spaces
# To print the blank lines using single quote
print(' ')
# No spaces
print("etutorialspoint")
# To print the blank lines using double quotes
print(" ")
# To print the spaces in sentence
print("etutorials point ")
# To print single space in sentence
print("etutorials","point ")
# To print single space in sentence
print("etutorials"+" "+"point ")
Output of the above code:
etutorialspoint
etutorials point
etutorials point
etutorials point
Python print multiple spaces
In the given Python program, we specify the number to repeat the number of spaces.
# Python program to print spaces
# by given number of times
print("etp"+" "*3+"etutorials"+" "*5+"point")
print("etp"+" "*2+"etutorials"+" "*5+"point")
Output of the above code:
etp etutorials point
etp etutorials point
Python add space between characters in a string
The join() method is a Python string method and returns a string by joining all the elements of an iterable, separated by a string separator. Here, we use this method, pass our string input and use space as a separator. It returns a string in which all items of an iterable are joined by a separator.
def removespaces(string):
return " ".join(string)
string = 'etutorialspoint'
# printing original string
print ("Original String: ", string)
print("New String: ", removespaces(string))
Output of the above code:
Original String: etutorialspoint
New String: e t u t o r i a l s p o i n t
Python add space between multiple strings
We can also use the join() method to add space between two or more strings. The given Python program returns a new string having the list items joined by a space.
str1 = "Hii"
str2 = "Hello"
str3 = "Welcome"
string = " ".join([str1, str2, str3])
print(string)
Output of the above code:
Hii Hello Welcome
Python add space at the beginning of a string
The rjust() method of Python is used to right align the string, using a specified character as the fill character. It returns the right-justified string within the given minimum width. If the fillchar is defined, it fills the remaining space with the defined character.
str1 = 'apple'
# print right justified string
result = str1.rjust(10, ' ')
print(repr(result))
Output of the above code:
' apple'
Python add space at the end of a string
The ljust() method will left align the string, using a specified character as the fill character. This method returns the left-justified string within the given minimum width.
str1 = 'apple'
# print left justified string
result = str1.ljust(10, ' ')
print(repr(result))
Output of the above code:
'apple '
Related Articles
Python Convert String to UppercaseConvert string to int Python
Python program to reverse a string
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Convert string to lowercase Python
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python iterate list with index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Strip function in Python
Python raise keyword
Check if two strings are anagrams Python
Python program to map two lists into a dictionary