Python remove leading and trailing white space from a String - Java @ Desk

Thursday, April 21, 2022

Python remove leading and trailing white space from a String

Python remove leading and trailing white space from a String

Python have 3 functions to remove the trimmed space, remove leading whitespace and remove trailing whitespace.

strip(): Removes leading and trailing whitespace.
rstrip(): Removes trailing whitespace.
lstrip(): Removes leading whitespace.

 string = '  Python Trim String  '  
 print('Original String is - \'' + string + '\'')  
 print('Removing Trailing Space - \'' + string.rstrip()+ '\'')  
 print('Removing Leading Space - \'' + string.lstrip()+ '\'')  
 print('Trimmed String is - \'' + string.strip()+ '\'')  


Output :

 Original String is - '  Python Trim String  '  
 Trimmed String is - 'Python Trim String'  
 Removing Trailing Space - '  Python Trim String'  
 Removing Leading Space - 'Python Trim String  '  






No comments:

Post a Comment