How Can We Help?
This tutorial will explain how to use the Python Strip method with basic syntax by example for better understanding.
How to Strip a String in Python? To strip a string in Python, we use the strip() method to remove particular characters or whitespaces of a string from the left and right directions, or we can say it performs both lstrip() and rstrip() on a string.
Syntax:
<string_value/string_variable>.strip(strip character)
OR
<string_value/string_variable>.strip()
Input Parameters:
- string_value / String variable: The string that we want to remove a particular character from.
- Strip Character: The character you want to eliminate from the string.
Python Strip Special Character
We Can Strip or remove leading and trailing special characters from a string by giving a special character argument to the strip function. ex: string.strip(‘$’).
Example (1)
# The String Value That We Want To Perform Strip Against
str1='$$$python36$$'
#Print String After Strip $ Character from it
print('Strip $: ',str1.strip('$'))
Output:
Strip $: python36
Explanation:
In the above example, ‘$’ exists at the start and the end in the string value of string variable ‘str1’, and we have removed them from the beginning and end by using str1.strip(‘$’) in the print statement.
Python Strip Whitespace
We Can Strip or remove leading and trailing white spaces from a string by calling the strip function without passing any argument. ex: string.strip().
Example (2)
# The String Value That We Want To Perform Strip Against
str2=' $$$python36$$ '
#Print String After removing whitespace from it
print('Strip:',str2.strip())
Output:
Strip: $$$python36$$
Explanation:
In the above example, white spaces exist at the start and the end in the string value of string variable ‘str1’, and we have removed them from the beginning and end by using str1.strip() in the print statement.
String Strip Newlines
We Can Strip or remove leading and trailing new lines from a string by giving a new line symbol as an argument to the strip function. ex: strip(‘\n’).
Example (3)
# The String Value That Contains newlines before and after the string
str3='\n$$$python36$$\n'
#Print String After removing newlines from it
print('str3 value after removing new line:',str3.strip('\n'))
Output:
Strip: $$$python36$$
Explanation:
In the above example, Newlines exist before and after the string value of string variable ‘str1’, and we have removed them from the beginning and end by specifying the argument ‘\n’, which refers to a new line to strip function in the print statement.
Python Strip Quotes
We Can Strip or remove leading and trailing quotes from a string by giving a quotation mark as an argument to the strip function. ex: strip(‘”‘).
Example (4)
# The String Value That Contains quotes before and after the string
str4='"Learn Python With Oraask"'
#Print String After removing quotes from it
print('str4 value after removing quotes:',str4.strip('"'))
Output:
str4 value after removing quotes: Learn Python With Oraask
Explanation:
In the above example, quotation marks exist before and after the string value of string variable ‘str1’; we have removed them from the beginning and end by specifying the argument ‘“‘ to strip function in the print statement.
In this tutorial, you have learned the string strip() method in Python. What is it? And how to remove a special characters, whitespaces, quotes and newlines from a string.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title(): It returns the “title cased” version of the string means all the words start with uppercase and the remaining are lowercase.
- capitalize(): It capitalizes the first character of the String.
- center(): It returns a space-padded string with the original string centered to a total of width columns.
- swapcase(): It inverts the case of all the characters in a string.
- upper(): It converts all the characters of a string to uppercase.
- lower(): It converts all the characters of a string to lowercase.
- rstrip(): It removes a particular character of a string from the right side.
- lstrip(): It removes a particular character of a string from the left side.
- ljust(): It returns an original string with a filled specific character or space at the left side of the string value to justify its new length (width).
- rjust(): It returns an original string with a filled specific character or space at the left side of the string value to justify its new length (width).
- zfill(): It returns an original string value with a filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)