Add Spaces Between Characters in Python

A string is the generic data type that is offered by Python to store and represent a series of Unicode characters. There might be a need to add a space between all characters of a given string. This tutorial focuses on and demonstrates the different ways available to add spaces between characters in Python.

The first part of the article focuses on how to add spaces between characters in Python while the latter half provides the solution on how to add spaces between numbers and characters in a string in Python.

How To Add Spaces Between Characters in Python?

There are several ways to add spaces between characters in Python, ranging from making use of the in-built join() function to iterating with the help of a for loop to importing an external library and utilizing its functions, all of which have been described in this article below.

Using the join() function

The str.join() function returns a new string that contains the concatenation of the strings given as the iterable.

When the join() function is called on an empty string that contains solely a space, while the specified string to be worked on is passed as an argument to the function, then, the task to add spaces between characters in Python can be successfully implemented.

The following code uses the join() function to add spaces between characters in Python.

Output:

J a v a 2 b l o g

Using a for loop

A simple for loop can be utilized to iterate through the given string and manually add a space between all the characters of the string in Python.

The following code uses a for loop to add spaces between characters in Python.

Output:

J a v a 2 b l o g

We should note that there may be a leading space in the output string as the for loop starts adding a space before the first character as well when the loop is run.

Using the strip() function along with a for loop

A little tweak in the above program and the addition of the strip() function make up for another approach that can successfully implement the task of adding spaces between characters in Python.

The strip() function can be utilized to get rid of any characters from the beginning or the end of a given string. In our case, we basically utilize the strip() function to truncate or remove any leading or trailing spaces that are a hindrance to the result that we need.

The following code uses the strip() function along with a for loop to add spaces between characters in Python.

Output:

‘J a v a 2 b l o g’

Using the replace() function

The replace() function in Python, as its name suggests, is utilized to replace a given phrase with another phrase, both of which are specified by the programmer and are passed as arguments to the function.

The following code uses the replace() function to add spaces between characters in Python.

Output:

J a v a 2 b l o g

How To Add Spaces Between Number and Character in Python?

We may encounter a string that contains both numbers as well as letters from the English alphabet. In those cases, there must be a need to add a space between the number and the character. This part of the article aims to provide the different approaches available to implement this task of adding spaces between a number and a character in Python.

Using the replace() function along with a nested for loop

The replace() function comes in handy in the implementation of this task as well. Apart from the replace() function, we also need a nested for loop to iterate through the strings and specify certain conditions that need to be pertained to in order to achieve an accurate result.

The following code uses the replace() function along with a nested for loop to add spaces between a number and a character in Python.

Output:

Java 2 blog

Using the re.sub() function

The re.sub() function is a part of the regex module, which needs to be imported to the Python code in order to utilize this function. The regex module is an abbreviation for Regular Expression and provides several functions to manipulate and deal with the same.

The re.sub() function takes an occurrence of a specified sub-string and replaces it with another sub-string. Here, we segregate the given string by targeting the numbers rather than the letters of the English Alphabet.

The following code uses the re.sub() function to add spaces between a number and a character in Python.

Output:

Java 2 blog

Using a lambda function along with the regex module

A lambda function can be defined as an anonymous function that is capable of having just one expression while taking in as many arguments as the user wants.

The re.sub() function is utilized along with the lambda function here to implement the task at hand successfully.

The following code uses a lambda function along with the regex module to add spaces between a number and a character in Python.

Output:

Java 2 blog

That’s all how to add spaces between characters in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *