Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Python Strings
Stripping substring
Python provides several methods to remove leading, trailing, or both leading and trailing whitespace from a string. Here are the most common ones:
- strip(): Removes both leading and trailing whitespace (or specified characters).
- lstrip(): Removes leading (left-side) whitespace (or specified characters).
- rstrip(): Removes trailing (right-side) whitespace (or specified characters).
1. strip(): Removing leading and trailing whitespace
The strip() method removes all leading and trailing whitespace by default. It can also be used to remove specific characters from the start and end of the string.
Syntax:
string.strip([chars])
chars (optional): A string specifying the set of characters to remove. If omitted, strip() will remove only whitespace.
Example: Removing whitespace
text = " Hello, World! " trimmed = text.strip() print(trimmed) # Output: "Hello, World!"
In this example, the leading and trailing spaces are removed.
Example: Removing specific characters
text = "###Python###" trimmed = text.strip("#") print(trimmed) # Output: "Python"
Here, strip("#") removes the # characters from both ends of the string.
2. lstrip(): Removing leading whitespace
The lstrip() method is used to remove whitespace (or specified characters) only from the beginning of the string (the left side).
Syntax:
string.lstrip([chars])
chars (optional): A string specifying the set of characters to remove from the beginning. If omitted, lstrip() will remove only whitespace.
Example: Removing leading whitespace
text = " Hello, World! " left_trimmed = text.lstrip() print(left_trimmed) # Output: "Hello, World! "
In this example, only the leading spaces are removed.
Example: Removing specific leading characters
text = "###Python###" left_trimmed = text.lstrip("#") print(left_trimmed) # Output: "Python###"
Here, only the "#" characters at the beginning of the string are removed.
3. rstrip(): Removing trailing whitespace
The rstrip() method is used to remove whitespace (or specified characters) only from the end of the string (the right side).
Syntax:
string.rstrip([chars])
chars (optional): A string specifying the set of characters to remove from the end. If omitted, rstrip() will remove only whitespace.
Example: Removing trailing whitespace:
text = " Hello, World! " right_trimmed = text.rstrip() print(right_trimmed) # Output: " Hello, World!"
In this example, only the trailing spaces are removed.
Example: Removing specific trailing characters
text = "###Python###" right_trimmed = text.rstrip("#") print(right_trimmed) # Output: "###Python"
Here, only the "#" characters at the end of the string are removed.
Conclusion:
These methods are useful for cleaning up strings in various contexts, such as when processing user input, text parsing, or formatting strings for display. Let me know if you have more questions!
It's time to take a quiz!
Test your knowledge and see what you've just learned.
Which Python method removes both leading and trailing whitespace (or specified characters) from a string?
Astrip()
Blstrip()
Crstrip()
Check Answer
Which Python method removes leading (left-side) whitespace (or specified characters) from a string?
Astrip()
Blstrip()
Crstrip()
Check Answer
Which Python method removes trailing (right-side) whitespace (or specified characters) from a string?
Astrip()
Blstrip()
Crstrip()
Check Answer
How do you remove specific characters (like #) from both ends of a string in Python?
Ctext.rstrip("#")
Atext.strip("#")
Btext.lstrip("#")
Check Answer
How do you remove specific characters (like #) from the left side of a string in Python?
Atext.strip("#")
Btext.lstrip("#")
Ctext.rstrip("#")
Check Answer