Python String strip() method
Python String strip() method
The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove).
Syntax
strip([chars])
Parameter
chars(optional): This parameter represents a string specifying the set of characters to be removed.
Return
This method returns a copy of the string with the leading and trailing characters removed.
Example 1
# Python program explaining # the string.strip() method # initializing the String with whitespaces as the leading characteres string = " Hello World" print("Actual String:",string) # removing the leading the characters val = string.strip() # printing the striped value print("After strip() method") print("String:",val, )
Output
Actual String: Hello World After strip() method String: Hello World
Example 2
# Python program explaining # the string.strip() method # initializing the String string = ' Python programing is easy to learn' # the Leading whitepsace are removed from the string print(string.strip()) # removing the leading and trailing characters print(string.strip('programing')) print(string.strip('Py thon')) web = 'https://www.tutorialsandexamples.com' print(web.strip('htps://.'))
Output
Python programing is easy to learn Python programing is easy to le programing is easy to lear www.tutorialsandexamples.com