How to remove space from string in Python
In this article, we will see how to remove space from string in Python.
Using replace()
# replace() method # to remove space in string def remove(s): return s.replace(" ", "") string = 'P yth o n poi nt' print(remove(string))
Output:
Pythonpoint
Using join() and split() method
# split() and join() method # to remove space in string def remove(s): return "".join(s.split()) string = 'P yth o n poi nt' print(remove(string))
Output:
Pythonpoint
Using Python regex
# python regex # to remove space in string import re def remove(s): t = re.compile(r'\s+') return re.sub(t, '', s) string = 'P yth o n poi nt' print(remove(string))
Output:
Pythonpoint
Subscribe
Login
Please login to comment
0 Discussion