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.
- 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.
- 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)
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.
str.replace(old, new, count)
- old: Original string.
- new: String will replace the original string.
- count: Specifies the maximum number of character substitutions.
- 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)
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.
newStr = orgStr.strip ( chars )
- 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.
re.sub(pattern, replace, string, count)
- 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.
- 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)
String contains a space: visit learnshare it String contains spaces: visit learnshare it
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:
- How to convert None to an Empty string in Python
- How to add space between variables in Python
- How To Replace Multiple Spaces With A Single Space In Python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Programming Languages: C++, Python, Java