To remove spaces, tabs and newlines from a String in Python, there are some solutions we have effectively tested. The methods in this article are to use the built-in functions in Python, like the re.sub() function, the combination of the join() function with str.split(), and list comprehension to remove excess spaces. Follow the article to understand better.
Remove spaces, tabs and newlines from a String in Python.
Use list comprehension.
Using list comprehension to remove spaces, tabs and newlines is a good idea. It’s fast and easy to implement.
Example:
# The string contains spaces, tabs, and newlines. StrExtraSpaces = 'Hello! Here is a \t lesson in Python\t tutorials on the \nLearnshareIt\n site.' # Use list comprehension to remove spaces, tabs, and newlines on the string. newStr = ''.join([i for i in StrExtraSpaces if i not in [' ', '\t', '\n']]) print(newStr)
Output:
Hello!HereisalessoninPythontutorialsontheLearnshareItsite.
Use RegEx.
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’.
- The re.sub function will search and replace spaces, tabs, and newlines in the original string.
import re # The string contains spaces, tabs, and newlines. StrExtraSpaces = 'Hello! Here \t is a lesson\n in Python tutorials on the \tLearnshareIt\n site.' # Use the re.sub() function to remove spaces, tabs, and newlines. newStr = re.sub(r"[\n\t\s]*", '', StrExtraSpaces) print(newStr)
Output:
Hello!HereisalessoninPythontutorialsontheLearnshareItsite.
Or use the pattern \\s+. The spaces, tabs and newlines will be removed.
Example:
import re StrExtraSpaces = 'Hello! Here is a \t lesson in Python\t tutorials on the \nLearnshareIt\n site.' # Use the re.sub() function to remove spaces, tabs and newlines. newStr = re.sub('\\s+', '', StrExtraSpaces) print(newStr)
Output:
Hello!HereisalessoninPythontutorialsontheLearnshareItsite.
Use the join() and split() functions.
You can use the join() function combined with the split() function to remove extra spaces on the string.
Syntax:
str.join(object)
Parameters:
- 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.
Syntax:
str.split(sep, maxsplit)
Parameters:
- sep: the character that splits the original string into a small string. The default is a space.
- maxsplit: maximum number of splits.
The split() function splits a string in Python with a delimiter and returns a list whose elements are just split strings.
Example:
- Create a string containing spaces, tabs, and newlines.
- Use the join() function combined with the split() function to remove spaces, tabs and newlines in a string.
StrExtraSpaces = 'This is the last\n example from \tthe same post.' # Use the join() function combined with the split() function to remove extra spaces, tabs and newlines on the string. newStr = "".join(StrExtraSpaces.split()) print(newStr)
Output:
Thisisthelastexamplefromthesamepost.
Summary
The article is all the knowledge I want to impart to Remove spaces, tabs and newlines from 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!

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
Major: IT
Programming Languages: C++, Python, Java