Given a string, write a Python program to remove all spaces from it.
Input : g e e k Output : geek Input : Hello World Output : HelloWorld
There are various approaches to remove whitespaces in a string. The first one is the Naive approach, which has been discussed in this article. But here we will discuss all the approaches which are specific to Python.
Approach#1 : Using
Using replace() function, we replace all whitespace with no space(“”).
Approach #2 : Using
First we use
split() function to return a list of the words in the string, using sep as the delimiter string. Then, we use
join() to concatenate the iterable.
Approach #3 : Using Python regex
Approach #4 : Using
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Remove unwanted spaces from string
- Python - Ways to remove multiple empty spaces from string List
- Python | Remove spaces from dictionary keys
- Python | Remove additional spaces in list
- Remove spaces from column names in Pandas
- Python | String Split including spaces
- Python | Check for spaces in string
- Python | Test if String contains Alphabets and Spaces
- Python - Avoid Spaces in string length
- Python code to move spaces to front of string in single traversal
- Replace missing white spaces in a string with the least frequent character using Pandas
- Python | Visualizing image in different color spaces
- Color Spaces in OpenCV | Python
- Python - Get number of characters, words, spaces and lines in a file
- Regex in Python to put spaces between words starting with capital letters
- Python program to count the number of blank spaces in a text file
- How to print spaces in Python3?
- How to split the element of a given NumPy array with spaces?
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.