Last Updated on July 2, 2023 by Mayank Dham
Python is a high-level programming language that is widely used for various applications, including data analysis, web development, and artificial intelligence. It is known for its simplicity, readability, and ease of use. One of the most commonly used functions in Python is the strip() function.
In this article, we will discuss the strip function in Python. We will explore what it is, how it works, and its various applications. We will also look at some examples to help you understand how to use the strip() function in your Python code.
What is the Strip() Function in Python?
The Python strip function in Python is used to remove the leading and trailing characters from a string. By default, the strip() function removes all white spaces from the beginning and end of the string. However, you can also specify which characters you want to remove by passing them as an argument to the strip() function.
The strip() function is a part of the string class in Python. Therefore, it can be called on any string object.
Syntax of Strip Function in Python
The syntax of the strip() function is as follows:
string.strip([characters])
Parameters of Strip Function in Python
The strip() function takes optional argument characters, which specifies the characters to be removed from the beginning and end of the string. If this argument is not provided, the strip() function removes all whitespace characters.
If the user does not provide the optional parameter characters, the strip() method will default to cutting the white spaces from the beginning and end of the actual string.
Return Type of Strip Function in Python
The strip Function in Python returns a duplicate of the original string after deleting the set of characters or whitespaces.
Examples of Strip Function in Python
Let’s see some examples to understand how the strip() function works.
Example 1: Using Strip Function in Python to Remove Whitespace Characters
The following code demonstrates how to use the strip() function to remove whitespace characters from the beginning and end of a string.
Code:
string = " PrepBytes! "
print(string.strip())
Output:
PrepBytes!
Explanation:
In the above code, we have a string " PrepBytes! " with leading and trailing whitespace characters. We use the strip() function to remove the whitespace characters from the beginning and end of the string. The output is the string "PrepBytes!".
Example 2: Using Strip Function in Python to Remove Specific Characters
The following code demonstrates how to use the strip() function to remove specific characters from the beginning and end of a string.
Code:
string = "+++PrepBytes!---"
print(string.strip('+-'))
Output:
PrepBytes!
Explanation:
In the above code, we have a string "+++PrepBytes!—" with leading and trailing plus and minus characters. We use the strip() function with an argument of ‘+-‘ to remove the plus and minus characters from the beginning and end of the string. The output is the string "PrepBytes!".
Example 3: Using Strip Function in Python with Variables
The following code demonstrates how to use the strip() function with variables.
Code:
name = " John " age = " 25 " print("Name:", name.strip()) print("Age:", age.strip())
Output:
Name: John
Age: 25
Explanation:
In the above code, we have two variables, name and age, with leading and trailing whitespace characters. We use the strip() function to remove the whitespace characters from the variables. The output is the variables name and age without whitespace characters.
rstrip() and lstrip() Functions in Python
In addition to the strip() function, Python also provides the rstrip() and lstrip() functions. These functions are similar to the strip() function, but they only remove characters from the right or left side of the string, respectively.
The rstrip() function removes the trailing characters from a string, while the lstrip() function removes the leading characters.
Let’s see some examples to understand how these functions work.
Example 1: Using rstrip() Function to Remove Trailing Characters
The following code demonstrates how to use the rstrip() function to remove trailing characters from a string.
Code:
string = "PrepBytes!+++"
print(string.rstrip('+'))
Output:
PrepBytes!
Explanation:
In the above code, we have a string "PrepBytes!+++" with trailing plus characters. We use the rstrip() function with an argument of ‘+’ to remove the plus characters from the end of the string. The output is the string "PrepBytes!".
Example 2: Using lstrip() Function to Remove Leading Characters
The following code demonstrates how to use the lstrip() function to remove leading characters from a string.
Code:
string = "---PrepBytes!"
print(string.lstrip('-'))
Output:
PrepBytes!
Explanation:
In the above code, we have a string "—PrepBytes!" with leading minus characters. We use the lstrip() function with an argument of ‘-‘ to remove the minus characters from the beginning of the string. The output is the string "PrepBytes!".
Conclusion
The strip() function in Python is a powerful tool for manipulating strings. It allows you to remove leading and trailing characters from a string, whether they are whitespace characters or specific characters that you specify. In this article, we have covered the syntax of the strip() function and provided examples of how to use it to remove whitespace characters and specific characters from a string. We have also discussed the rstrip() and lstrip() functions, which allow you to remove characters from the right or left side of a string, respectively.
So, the strip() function is an essential tool for any Python programmer who works with strings. It is a simple yet powerful function that can make your code more efficient and easier to read.
Strip Function in Python – FAQs
Here are some frequently asked questions (FAQ) on the strip() function in Python:
Ques 1. What characters does the strip() function remove by default?
Ans. By default, the strip() function removes leading and trailing whitespace characters, including spaces, tabs, and newline characters.
Ques 2. Can I use the strip() function to remove specific characters from a string?
Ans. Yes, you can use the strip() function to remove specific characters from a string by passing those characters as an argument to the function. For example, to remove all instances of the letter ‘a’ from a string, you can use the strip() function as follows:
string.strip('a')
Ques 3. How does the strip() function differ from the replace() function?
Ans. The strip() function removes characters only from the beginning and end of a string, while the replace() function replaces all occurrences of a character or substring with another character or substring.
Ques 4. Enlist the differences between the strip(), lstrip(), and rstrip() functions provided in Python?
Ans. The strip() function removes characters from both the beginning and end of a string, while the lstrip() function removes characters only from the beginning, and the rstrip() function removes characters only from the end.
Ques 5. Can the strip() function modify the original string?
Ans. No, the strip() function does not modify the original string. Instead, it returns a new string with the leading and trailing characters removed.
Ques 6. How can I remove only leading or trailing characters from a string?
Ans. To remove only leading characters from a string, you can use the lstrip() function, and to remove only trailing characters, you can use the rstrip() function.