Lecture
Removing Leading and Trailing Whitespace with the strip() Function
The strip() function removes whitespace (such as spaces and tabs) from both the beginning and end of a string.
Example of using the strip() function
text = " Hello, nice to meet you! " stripped_text = text.strip() print(stripped_text) # "Hello, nice to meet you!"
This function is commonly used to clean up user input by removing unnecessary spaces and normalizing the text.
Removing Specific Characters
You can also remove specific characters from both ends of a string by passing them as an argument to the strip() function.
Example of removing specific characters
text = "xxxHello, nice to meet you!xxx" stripped_text = text.strip('x') # "Hello, nice to meet you!" print(stripped_text)
Previous lessonCase Conversion Methods upper() and lower()Next lessonString Composition Verification with isOOO() Methods
Lessons in this chapter · String Formatting Made Easy
- 1. Inserting Variables into Strings with the format() Function
- 2. Tips for Using `format()` Function - indexError
- 3. Displaying Integers with the format() Function
- 4. Handling Floating-Point Numbers with the format() Function
- 5. Multiple-choice quiz
- 6. Case Conversion Methods upper() and lower()
- 7. Removing Leading and Trailing Whitespace with the strip() Function
- 8. String Composition Verification with isOOO() Methods
- 9. Finding the Position of a Specific Character Using find() and rfind()
- 10. Using the split() Method for Strings
- 11. How to Check if a Specific Character Exists in a String
- 12. Multiple-choice quiz
- 13. How to Format Strings with f-Strings
- 14. Comparison of format() Function and f-Strings
- 15. Using the `input` Function to Receive User Input
- 16. Indicating Invalid Values with ValueError
- 17. Coding Quiz - Splitting Strings
- 18. Multiple-choice quiz
- 19. Fill-in-the-blank quiz
Quiz
0 / 1
The strip() function can only remove whitespace from the beginning of a string.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help