python remove empty space from string code example

Example 1: delete space in string python

.replace(" ", "")

Example 2: remove extra spaces and empty lines from string python

"\n".join([s for s in code.split("\n") if s])

Example 3: python delete white spaces

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

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 remove spaces from string

>>> " ".join(s.split())
'Hello World From Pankaj Hi There'

Tags:

Java Example