1import re
2
3string = ' Hello World From Pankaj \t\n\r\tHi There '
4
5print('Remove all spaces using RegEx:\n', re.sub(r"\s+", "", string), sep='') # \s matches all white spaces
6print()
7
8print('Remove leading spaces using RegEx:\n', re.sub(r"^\s+", "", string), sep='') # ^ matches start
9print()
10
11print('Remove trailing spaces using RegEx:\n', re.sub(r"\s+$", "", string), sep='') # $ matches end
12print()
13
14print('Remove leading and trailing spaces using RegEx:\n', re.sub(r"^\s+|\s+$", "", string), sep='') # | for OR condition
15