import re
s = '
this is a string with a lot of whitespace '
s = re.sub('s+', '', s)
' hello world! '.strip()
'hello world!'
' hello world! '.lstrip()
'hello world! '
' hello world! '.rstrip()
' hello world!'
>>> " xyz ".rstrip()
' xyz'
' sss d ssd s'.replace(" ", "")
# output: 'sssdssds'
sentence = ' hello apple'
" ".join(sentence.split())
>>> 'hello apple'
>>> s.strip()
'Hello World From Pankaj
Hi There'
s = ' This is a sentence with whitespace.
'
print('Strip leading whitespace: {}'.format(s.lstrip()))
print('Strip trailing whitespace: {}'.format(s.rstrip()))
print('Strip all whitespace: {}'.format(s.strip()))
# Output
# Strip leading whitespace: This is a sentence with whitespace.
# Strip trailing whitespace: This is a sentence with whitespace.
# Strip all whitespace: This is a sentence with whitespace.
def remove_witespace(data_of_string):
string = ""
for char in data_of_string:
if " " not in char:
string = string + char
return string
print(remove_witespace("python is amazing programming language"))
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()}'')
vals_inp=input()
list_set = list(vals_inp)
vals = [x for x in list_set if x != ' ']
set_vals = set(vals)
Use the lstrip() method
>>> name = ' Steve '
>>> name
' Steve '
>>> name = name.lstrip()
>>> name
'Steve '