how to delete whitespace in python code example

Example 1: python remove whitespace from start of string

'     hello world!    '.strip()
'hello world!'


'     hello world!    '.lstrip()
'hello world!    '

'     hello world!    '.rstrip()
'    hello world!'

Example 2: delete spaces in string python

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

Example 3: how to remove spaces in string in python

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

Example 4: python remove spaces

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

Example 5: how to strip white space of text in python?

sentence = ' hello  apple'
" ".join(sentence.split())
>>> 'hello apple'