Python string | strip()
Last Updated :
08 Sep, 2023
Python String strip() is an inbuilt function in the Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). This article will examine the many features and use cases of the strip() method to give you a thorough grasp of how to use it successfully in your Python programs.
Python strip() Method Syntax
Syntax: string.strip([chars])
Parameter: There is only one optional parameter in it. chars – a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string.
Return Value: Returns a copy of the string with both leading and trailing characters removed.
Purpose of the Python Strip() Function
When a developer wishes to remove characters or whitespaces from the beginning or end of a string, the Strip() function in Python comes in handy. Let’s take a closer look at it:
- The strip() function assists in removing characters from the beginning or end of a string for characters supplied as arguments to the strip() function ().
- If the string has no whitespaces and the characters argument is not supplied, the string is returned as is.
- It is also beneficial to eliminate whitespaces from the beginning and end of the text.
- If the string contains whitespaces and no character arguments are supplied, the string will be returned after discretizing the whitespaces.
String strip() in Python Example
In Python, the strip()
method is used to remove leading and trailing whitespace characters (spaces, tabs, and newlines) from a string. It returns a new string with the whitespace characters removed. The original string remains unchanged.
Example
Python3
my_string = " Hello, world! "
stripped_string = my_string.strip()
print (stripped_string)
|
Output
Hello, world!
Python Stripping String with Strip() function
In this example, we will Python String Trim and we have used a string and we have applied the strip() function with a string and without a string.
Python3
string =
print (string)
print (string.strip())
print (string.strip( ' geeks' ))
|
Output
geeks for geeks
geeks for geeks
for
Python Stripping Specific Character with Strip() Function
In this example, we will Python String Trim and we have used the strip() function to remove a specific set of characters from a string.
Python3
str1 = 'geeks for geeks'
print (str1)
str2 = 'ekgs'
print (str1.strip(str2))
|
Output
geeks for geeks
for
Python Removing Whitespaces with Strip() function
In this example, we will Python String Trim and we have used the strip() function to remove the whitespaces from both ends of a string.
Python3
str1 =
print (str1)
print (str1.strip())
|
Input
geeks for geeks
Output
geeks for geeks
Python Removing NewLine using Strip() Function
In this example, we will Python String Trim and we are using the strip() function to remove the newline characters from a string.
Python3
string = "\nHello, World!\n"
new_string = string.strip()
print (new_string)
|
Output
Hello, World!
Practical Application
Given a string remove the occurrence of the word “the” from the beginning and the end. we will Python String Trim.
Python
string = " the King has the largest army in the entire world the"
print (string.strip( " eht" ))
|
Input
the King has the largest army in the entire world the
Output
King has the largest army in the entire world
Please Login to comment...