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?

strip()
lstrip()
rstrip()
Check Answer

Which Python method removes leading (left-side) whitespace (or specified characters) from a string?

strip()
lstrip()
rstrip()
Check Answer

Which Python method removes trailing (right-side) whitespace (or specified characters) from a string?

strip()
lstrip()
rstrip()
Check Answer

How do you remove specific characters (like #) from both ends of a string in Python?

text.rstrip("#")
text.strip("#")
text.lstrip("#")
Check Answer

How do you remove specific characters (like #) from the left side of a string in Python?

text.strip("#")
text.lstrip("#")
text.rstrip("#")
Check Answer