1s = ' Hello World From Pankaj \t\n\r\t Hi There '
2
3>>> s.replace(" ", "")
4'HelloWorldFromPankaj\t\n\r\tHiThere'
1import re
2s = '\n \t this is a string with a lot of whitespace\t'
3s = re.sub('\s+', '', s)
1' hello world! '.strip()
2'hello world!'
3
4
5' hello world! '.lstrip()
6'hello world! '
7
8' hello world! '.rstrip()
9' hello world!'
1## Remove the Starting Spaces in Python
2
3string1=" This is Test String to strip leading space"
4print (string1.lstrip())
5
1
2s1 = ' abc '
3
4print(f'String =\'{s1}\'')
5
6print(f'After Removing Leading Whitespaces String =\'{s1.lstrip()}\'')
7
8print(f'After Removing Trailing Whitespaces String =\'{s1.rstrip()}\'')
9
10print(f'After Trimming Whitespaces String =\'{s1.strip()}\'')
11