Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. How to add spaces in Python

How to add spaces in Python

Author image

Harsh Pandey

Software Developer

Published on Tue Mar 26 2024

Introduction

Adding spaces for formatting the text is easier in Python as compared to other languages as it offers various methods to achieve this task. To put spaces between words, we can use the "join" method. For adding spaces between characters, we can use a for-loop to go through the string and add spaces. To add spaces at the end and beginning of a string, we can use the "ljust" and “rjust” methods respectively.

Through this blog - “How to add spaces in Python”, let's explore these methods in detail using easy-to-follow examples!

How to add spaces in Python between words in a string

Method 1: Using Join() Method

The join() method joins a sequence of strings with a specified delimiter. To add spaces between words, we can split the string into words using split() method, and then rejoin them with spaces using ' '.join(words).

sentence = "PythonProgrammingIsFun"
words = sentence.split("Is")
modified_sentence = ' '.join(words)
print(modified_sentence)  # Output: "PythonProgramming Is Fun"

In the above code example, we split the sentence string into words using split() with "Is" as the delimiter. Then, we use ' '.join(words) to join the words back together with a space between them, creating the modified_sentence with spaces.

Method 2: Using ‘+’ Operator

We can also add spaces between words by using the ‘+’ operator to concatenate them with a space in between.

sentence1 = “Add”
sentence2 = “Spaces”
sentence3 = “Python”
print(sentence1 + “ “ + sentence2 + “ “ + sentence3)  # Output: “Add Spaces Python”

When using the ‘+’ operator, ensure that both operands on the left and right-hand sides are strings.

Method 3: Using f-string syntax

In Python, f-strings (formatted strings) are a convenient way to create strings with dynamic content. We can use f-strings to insert expressions, variables, or even expressions involving multiple variables directly into a string. To add spaces between words in a string using f-string, we first split the original string into words, and then we construct a new string using f-string syntax with spaces between the words.

sentence = "PythonProgrammingIsFun"
words = sentence.split("Is")
modified_sentence = f"{words[0]} Is {words[1]}"  # f-string syntax
print(modified_sentence)  # Output: "PythonProgramming Is Fun"

In the above example code, we split the sentence into a list of words using the split() method, with "Is" as the delimiter. As a result, the words list now contains two elements: "PythonProgramming" and "Fun." To create the modified_sentence, we use f-string syntax within the curly braces {}. Inside these braces:

  • words[0] represents the first word, which is "PythonProgramming."
  • " Is " is a string containing spaces on both sides, effectively adding spaces between the words.
  • words[1] represents the second word, which is "Fun."

Upon evaluation, the f-string substitutes the expressions inside the curly braces with their respective values. This yields a new string, modified_sentence, containing spaces between the words: "PythonProgramming Is Fun."

How to add spaces in Python between characters in a string

Method 1: Using Join() Method

We can use the join() method to join a sequence of characters with a specified delimiter. Here, we'll use it to add spaces between each character of a string.

word = "Python"
spaced_word = ' '.join(word)
print(spaced_word)  # Output: "P y t h o n"

Method 2: Using for loop

We can achieve the same result of adding spaces between each character of a string using a for loop to iterate through each character and add spaces between them.

word = "Python"
spaced_word = ‘’
for char in word:
    spaced_word += char + ' '
print(spaced_word.rstrip())  # Output: "P y t h o n"

Note: rstrip() method is used to remove extra whitespaces at the end of the string.

Method 3: Using replace() method

Although it's not the most efficient way, we can use replace() method to add spaces between characters of a string.

word = "Python"
spaced_word = word.replace("", " ").strip()
print(spaced_word)  # Output: "P y t h o n"

Note: strip() method is used to remove extra whitespaces at both the ends of the string.

How to add spaces in Python to the end of a string

Method 1: Using ljust() Method

The ljust() method is used for left-justifying a string within a specified width, padding spaces on the right if necessary.

text = "Python"
padded_text = text.ljust(10)
print(padded_text)  # Output: "Python    "

How to add spaces in Python to the beginning of a string

Method 1: Using rjust() Method

The rjust() method returns a right-justified version of the original string, padding the left side with spaces to achieve the desired width.

text = "Python"
padded_text = text.rjust(10)
print(padded_text)  # Output: "    Python"

Using Multiplication Operator

In Python, the multiplication operator * can be used with strings to repeat the string a specified number of times, useful for adding spaces at the beginning or end of a string.

# Add spaces at the beginning of the string
text = "Python"
padding_spaces = 5
padded_text = ' ' * padding_spaces + text
print(padded_text)  # Output: "     Python"

# Add spaces at the end of the string
text = "Python"
padding_spaces = 5
padded_text = text + ' ' * padding_spaces
print(padded_text)  # Output: "Python     "

Conclusion

Through this blog - “How to add spaces in Python”, we have explored various methods to add spaces between words and characters, as well as padding at the beginning and end of strings. Methods like `join()`, `for` loop, and `replace()` are effective for adding spaces between characters, while `ljust()`, `rjust()`, and the multiplication operator provide options for padding. These capabilities make Python an excellent choice for text formatting and achieving visually appealing displays.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.