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.