Python Program to Trim Whitespace From a String

 

Example 1: Using strip()

my_string = " Python "

print(my_string.strip())

Example 2: Using regular expression

import re

my_string  = " Hello Python "
output = re.sub(r'^\s+|\s+$', '', my_string)

print(output)


0 Comments