remove all spaces from a string python code example

Example 1: replacing spaces in a string python

mystring.replace(" ", "_")

Example 2: delete space in string python

.replace(" ", "")

Example 3: remove all whitespace from string python

import re
s = '\n \t this is a string   with a lot of whitespace\t'
s = re.sub('\s+', '', s)

Example 4: delete spaces in string python

>>> s.replace(" ", "")

Example 5: how to remove spaces in string in python

sentence = '       hello  apple         '
sentence.strip()
>>> 'hello  apple'

Example 6: python remove spaces

string=' t e s t ' 
print(string.replace(' ',''))