How to Add a Space in Python?


Have you ever wanted to add a space in between words while using the print statement in Python? If so, you’re in luck! This process is actually really simple. All you have to do is type in print(” “), and then whatever you want to print after that. You can even put this inside of a string if you want to! Let’s take a look at an example.

To Add a space using the print statement

In order to add a space in between words while using the print statement, all you have to do is type in print(” “), and then whatever you want to print after that. You can even put this inside of a string if you want to! Let’s take a look at an example.

If we wanted to add a space in between the words “Hello” and “World”, we would type in the following:

print("Hello" + " " + "World")
Code language: PHP (php)

And the output would be as follows:

Hello World

As you can see, there is now a space in between the two words! You can use this same method for any other string manipulation that you might want to do.

How to use rjust() in Python to add a space at the beginning of a string.

Python’s rjust() method gives you a new string. As an input parameter, you give the length of the new string. Adding characters to the left side of the original string makes it longer.

string.rjust(length, character)
Code language: CSS (css)
  • length: This is the length of the string that has been changed. If the given length is less than or the same as the length of the original string, the original string is returned.
  • character: You don’t have to use the character parameter. The given character is used to fill out the string’s left side. The value of character is set to “space” by default.

Example

# Python program to add space at beginning of the string using rjust() method # Defining the string demo_string = "Apple" # required length of string after adding space # where 20 is number of space to be added at beginning of the string required_length = 20 # Using rjust() method # Not providing padding character becuase default character is space modified_string = demo_string.rjust(required_length) # Printing modified_string print(modified_string)
Code language: Python (python)

Output

Apple

Using ljust() method to add space at end of the string in python

>>> mystr = 'Hi' >>> mystr.ljust(4) 'Hi ' >>> mystr.ljust(4, '-') 'Hi--' >>> mystr.ljust(2, '-') 'Hi'
Code language: JavaScript (javascript)

Using center() method to add space at both ends of the string in python

Example

txt = 'DisneyLand' new_str = txt.center(20) print('Centered string:', new_str) new_str = txt.center(20, '^') print('Centered string:', new_str)
Code language: PHP (php)

Output

Centered string: Disneyland Centered string: ^^^^^Disneyland^^^^^

There are various ways to add a space between variables in python. We can use the print() function and list the variables separate them by using a comma or by using the format() function. Another way to add space between variables is to use the rjust(), ljust(), center() methods.

These methods will add space between the variable and other characters. The number of spaces added can be specified as an argument for these functions. For example, we can use rjust(10) to add 10 spaces at beginning of the string. This can be helpful when creating formatted strings.

Hopefully, this article helped shed some light on how easy it is to add a space between words.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts