python delete spaces from string code example

Example 1: delete space in string python

.replace(" ", "")

Example 2: remove spaces from string python

s = '  Hello   World     From  Pankaj  \t\n\r\t  Hi       There        '

>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'

Example 3: delete spaces in string python

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

Example 4: how to remove spaces in string in python

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

Example 5: python remove space from end of string

>>> "    xyz     ".rstrip()
'    xyz'

Example 6: python strip whitespace

s1 = '  abc  '

print(f'String =\'{s1}\'')

print(f'After Removing Leading Whitespaces String =\'{s1.lstrip()}\'')

print(f'After Removing Trailing Whitespaces String =\'{s1.rstrip()}\'')

print(f'After Trimming Whitespaces String =\'{s1.strip()}\'')