To remove Whitespaces from String in Python?

In this blog, We are going to learn How to remove White Spaces from String in Python?

There are various methods to remove whitespaces in a string. Any function that manipulates string value returns a new string and we have to explicitly assign it to the string, otherwise, the string value won’t change.

Different Methods:

join()

>>> a='aa bb cc dd ee ff'
>>> a1=''.join(a.split())
>>> a1
Output: aabbccddeeff

list comprehension()

>>> a='aa bb cc dd ee ff'
>>> a1=str(''.join(([i for i in a if i!=' '])))
>>> a1
Output: aabbccddeeff

replace()

>>> a='aa bb cc dd ee ff'
>>> a1 = a.replace(' ','')
>>> a1
Output: aabbccddeeff

I hope that you found this Blog helpful. If you’ve any queries, please leave comments in the comment section.

Thanks for following and reading this blog.

To remove Whitespaces from String in Python?expoundsofttechsolution

To remove Whitespaces from String in Python?

kj