Python
Python OOPs: Class, Object, Inheritance and Constructor with Example
OOPs in Python OOPs in Python is a programming approach that focuses on using objects and classes...
Python strip() function is a part of built-in functions available in the Python library. The strip() method removes given characters from start and end of the original string. By default, strip() function removes white spaces from start and end of the string and returns the same string without white spaces.
In this Python tutorial, you will learn:
string.strip([characters])
The Python String strip() will return:
str1 = "Welcome to Guru99!" after_strip = str1.strip()
Output:
Welcome to Guru99!
The Python String strip() function works only on strings and will return an error if used on any other data type like list, tuple, etc.
Example when used on list()
mylist = ["a", "b", "c", "d"] print(mylist.strip())
The above will throw an error :
Traceback (most recent call last):
File "teststrip.py", line 2, in <module>
print(mylist.strip())
AttributeError: 'list' object has no attribute 'strip'
str1 = "Welcome to Guru99!" after_strip = str1.strip() print(after_strip) str2 = "Welcome to Guru99!" after_strip1 = str2.strip() print(after_strip1)
Output:
Welcome to Guru99! Welcome to Guru99!
str1 = "****Welcome to Guru99!****"
after_strip = str1.strip("*")
print(after_strip)
str2 = "Welcome to Guru99!"
after_strip1 = str2.strip("99!")
print(after_strip1)
str3 = "Welcome to Guru99!"
after_strip3 = str3.strip("to")
print(after_strip3)
Output:
Welcome to Guru99! Welcome to Guru Welcome to Guru99!
Here, are reasons for using Python strip function
OOPs in Python OOPs in Python is a programming approach that focuses on using objects and classes...
Python abs() Python abs() is a built-in function available with the standard library of python. It...
How to Use Python Online Compiler Follow the simple steps below to compile and execute Python...
What is Python 2? Python 2 made code development process easier than earlier versions. It...
Dictionary is one of the important data types available in Python. The data in a dictionary is...
What is Python Enumerate? Python Enumerate() is a buit-in function available with the Python...