string without space pythonm

Solutions on MaxInterview for string without space pythonm by the best coders in the world

showing results for - "string without space pythonm"
Benjamín
17 Oct 2020
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