Python String strip()

The Python String strip() method is a built-in function that strips both leading and trailing characters based on the arguments passed to the function and returns the copy of a string.

In this article, we will learn about the Python String strip() method with the help of examples.

If we want to remove only the leading characters in a string, we could use Python String lstrip(). Similarly, if we want to strip only the trailing characters, we could use Python String rstrip() method.

strip() Syntax

The Syntax of strip() method is:

string.strip([chars])

strip() Parameters

The strip() method takes one parameter, and it’s optional.

  • chars(optional) – set of characters representing string that needs to be removed from both left and right-hand sides of the string. 

If the chars argument is not passed, the strip() function will strip whitespaces at the start and end of the string. 

strip() Return Value

The strip() method returns a copy of the string by stripping both leading and trailing characters based on the arguments passed.

Note: 

  • If we do not pass any arguments to strip() function, by default, all the leading and trailing whitespaces are truncated from a string.
  • If the string does not have any whitespaces at the start or end, the string will be returned as-is, matching the original string.
  • If the characters passed in the arguments do not match the characters at the beginning of the string, it will stop removing the leading characters.
  • Similarly, if the characters passed in the arguments do not match the characters at the end of the string, it will stop removing the trailing characters.

Example: Working of the strip() method

Below are the various working example of the strip() method. We can use it to remove whitespace or we can strip the characters at both leading and trailing end.

  • text1.strip() – Removes the whitespace at both leading and trailing of the string
  • text3.strip(' code') – Remove the whitespace and the substring code from both leading and trailing end
  • text2.strip('code') – Removes only the substring from both leading and trailing end.
  • text4.strip('The') – Removes the substring only at the leading end.
# Leading and trailing whitespaces are removed
text1 = '  Python Programming   '
print(text1.strip())


# Remove the whitespace and specified character on
# both leading and trailing end
text3 = '       code its my code        '
print(text3.strip(' code'))

# Remove the specified character at 
# both leading and trailing end
text2 = 'code its my code'
print(text2.strip('code'))

# strips only the beginning 
# of the string 
text4="The Coding is fun"
print(text4.strip('The'))

Output

Python Programming
its my
 its my
 Coding is fun

Example 2 – How to use strip() method in real world?

Suppose we are fetching data from external sources like Excel, DB or third party APIs. In that case, there are higher chances that data is not formed correctly, and we may get the separators like pipe, comma, hyphen etc., appended to the string. We can use the strip() method to remove those special characters and preserve the original value.

In the below example, we have a list of languages and the hyphen is appended at both the trailing and leading end of each element. We can simply iterate the list and remove the hyphen using the strip() method as shown below.

langs = ['-Python-', '-Java-', '-Javascript-', '-C#-', '-C++-']
new_langs = []
for l in langs:
    new_langs.append(l.strip('-'))

print(new_langs)

Output

['Python', 'Java', 'Javascript', 'C#', 'C++']
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Jsonpath

Python JSONPath

Table of Contents Hide JSONPath Library in PythonInstalling jsonpath-ng ModuleJsonpath operators:Parsing a Simple JSON Data using JSONPathParsing a Json Array using JSONPath Expression JSONPath is an expression language that is…
View Post
Indentationerror Unexpected Indent

IndentationError: unexpected indent

Table of Contents Hide What are the reasons for IndentationError: unexpected indent?Python and PEP 8 Guidelines Solving IndentationError: expected an indented blockExample 1 – Indenting inside a functionExample 2 – Indentation…
View Post
Calculate Standard Error In R

Calculate Standard Error in R

Table of Contents Hide How to Calculate Standard Error in R?Using sd() methodUsing the standard error formulaUsing std.error() method from plotrix The standard error (SE) of a statistic is the standard deviation of its sampling…
View Post
Python Ternary Operator

Python Ternary Operator

Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the…
View Post