python how to remove white spaces from string code example

Example 1: remove trailing and leading spaces in python

text = "   Hello World        "
text.strip()
# Hello World

Example 2: python remove whitespace from start of string

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


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

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

Example 3: python delete white spaces

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

Example 4: delete spaces in string python

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

Example 5: python string remove whitespace

' sss d ssd s'.replace(" ", "")
# output: 'sssdssds'