How To Add Spaces Between The Characters Of A String In Python

Add spaces between the characters of a string in Python

To add spaces between the characters of a string in Python, you can use three ways: the join() function, the replace() function and Regular Expression. Read the following article for more information.

Add spaces between the characters of a string in Python

Use the join() function 

You can use the join() function to add spaces between the characters of a string.

Syntax:

str.join(object)

Parameters:

  • str: Delimiter for string concatenation.
  • object: List or tuple containing elements that are strings to be concatenated.

The join() function returns an object that is a character string of the elements of the specified list or tuple joined together by a delimiter.

Example:

  • Create a string.
  • Call join() on a string of spaces.
  • The argument to join() is the string you initialize.
  • The join() function will return a string whose characters are separated by a space.
myStr = 'visitlearnshareitwebsite'

# Use the join() function to add spaces
resultStr = ' '.join(myStr)
print(resultStr)

Output:

v i s i t l e a r n s h a r e i t w e b s i t e

Use the replace() function

The replace() function also has a good way you can add spaces.

Syntax:

str.replace(old, new, count)

Parameters:

  • old: Original string.
  • new: String will replace the original string.
  • count: Specifies the maximum number of character substitutions.

Example:

  • Create a string.
  • Use the replace() function to add spaces to the string.
  • After adding spaces, the string appears as extra spaces at the beginning, and at the end of the string, I use the strip() function to remove it.
myStr = 'learnshareit'

# Use the replace() function to add spaces
mySpaceStr = myStr.replace("", "  ")[1: -1]

print('String has not removed leading and trailing spaces:')
print(mySpaceStr)

# Use the strip() to remove leading and trailing spaces
resultStr = mySpaceStr.strip()

print('String after removing the leading and trailing whitespace:')
print(resultStr)

Output:

String has not removed leading and trailing spaces:
 l  e  a  r  n  s  h  a  r  e  i  t 
String after removing the leading and trailing whitespace:
l  e  a  r  n  s  h  a  r  e  i  t

Note: The strip() function

The strip() function deletes the string’s first character at both ends and returns a new string.

Syntax:

newStr = orgStr.strip ( chars )

Parameters:

  • newStr: New return returned after removing the character.
  • orgStr: The original string.
  • char: The set of characters you want to remove at both ends of the string orgStr.

Use Regular Expression

Regular Expression (RegEx), a.k.a a regular expression, is a sequence of special characters that add a particular pattern, representing strings or a collection of strings.

In Python, Regular Expression is expressed through the ‘re’ module, so you have to import module ‘re’ before you want to use Regular Expression. Module ‘re’ has many methods and functions to work with RegEx, but one of the essential methods is ‘re.sub’.

The Re.sub() method will replace all pattern matches in the string with something else passed in and return the modified string.

Syntax:

re.sub(pattern, replace, string, count)

Parameters:

  • pattern: is RegEx.
  • replace: is the replacement for the resulting string that matches the pattern.
  • string: is the string to match.
  • count: is the number of replacements. Python will treat this value as 0, match and replace all qualified strings if left blank.

Example:

  • Import module re.
  • Create a string.
  • Use the re.sub()  function to add spaces to the string.
import re

# Initializing string contains a space
myStr = 'visit learnshare it'

print('String contains a space:', myStr)

# Use the sub() function to add spaces
resultStr = re.sub("\s+", '       ', myStr)

print('String contains spaces:', resultStr)

Output:

String contains a space: visit learnshare it
String contains spaces: visit       learnshare       it

Summary

Here are the solutions that can help you add spaces between the characters of a string in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

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