This Python 3 tutorial will show you how to trim strings. I’ll go over some scenarios for trimming strings in Python. Trimming the string helps to remove the white gaps. The leading and following spaces in a Python string can be removed.
The python has string package to handle string manipulation. You can also checkout other python tutorials:
- How To Convert Python String To Array
- Python String join Example
- Python Array of Strings
- How To Match String Item into List Python
- How To Convert String to int and int to string
- Python re match Example
- How To Use Regex With Python
The Python provides three methods that can be used to trim white-spaces from the string object.
- strip(): It returns a new string after removing any leading and trailing whitespaces including tabs (\t).
- rstrip(): It returns the new string with the trailing white space removed. It’s easier to remember by removing the white spaces from a “right” side of a string.
- lstrip(): It returns the new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.
All of these methods don’t accept any arguments to remove whitespaces. If a character argument is provided, then they will remove those characters from the string from leading and trailing places.
Python strip() example
Python strip()
method is used to remove all the leading and trailing whitespace characters such as \n, \r, \t, \f, space. This method returns the copy of the string in which tripped all characters from the beginning and the end of the string.
Simple Uses of Python strip Method
We will write below code into the app.py
file –
name = ' pythonpip ' print('Data after trim =', name) print('After Trimming Whitespaces String =',name.strip())
Data before trim = pythonpip After Trimming Whitespaces String = pythonpip
The white spaces at the beginning and end of the string have been eliminated using the Python strip()
method, as shown in the preceding result.
Now, let’s pass the parameter and see the output.
name = ' pythonpip' print('Data before trim =', name) print('After Trimming Whitespaces String =',name.strip('p'))
Output –
Data before trim = pythonpip After Trimming Whitespaces String = pythonpi
How To Remove Special Character From String Using Python
The special character can also be removed or trimmed from the string. The special character must be passed as a parameter to the python strip()
method.
name = '#p@ythonpip,,,' print('Data before trim =', name) print('After Trimming Whitespaces String =',name.strip('#,@'))
Output –
Data before trim = #p@ythonpip,,, After Trimming Whitespaces String = p@ythonpip
The strip
method has been used to remove the # and comma from the beginning and end of the string, as you can see from the result above.
Python lstrip() example
The lstrip() function in Python is used to remove left side whitespace characters from a string, such as \n
, \r
, \t
, \f
, and space
. This method returns a replica of the string in which all characters from the start have been tripped.
Simple Uses of Python lstrip Method
To trim the string in Python, add the following code to the app.py
file.
name = ' pythonpip' print('Data after trim =\'{name}\'') print('After Left trim Whitespaces from String =\'{name.strip()}\'')
Output :
Data before trim = pythonpip After Trimming Whitespaces String = pythonpip
You can see in the above output that the Python strip()
method has been removed the white spaces from the beginning of the string.
Python rstrip() example
The rstrip() method in Python is used to remove right-side whitespace characters from a string, such as \n, \r, \t, \f, space. This method returns the copy of the string in which tripped all characters from the end of the string.
Simple Uses of Python lstrip Method
We will write below code into the app.py
file –
name = ' pythonpip ' print('Data after trim =', name) print('After Trimming Whitespaces String =',name.strip())
Output :
Data before trim = pythonpip After Trimming Whitespaces String = pythonpip
You can see in the above output that the Python strip() method has been removed the white spaces from the end of the string.