Python delivers three methods that you can utilize to trim whitespace from a string and return a new string object. The string strip methods can trim showing whitespace, trailing whitespace, or both.
Whitespace contains all Unicode whitespace characters, such ascarriage returns (\r),spaces, tabs (\t) and newlines (\n). The Python str() class has the following ways that you can use to trim whitespace from a string:
- rstrip([chars]): Trims characters from the right side of a string. When chars is omitted or None, returns a new string with all trailing whitespace emptied.
- strip([chars]): Trims symbols from both ends of a string. When chars is omitted or None, returns a new string with all showing and trailing whitespace removed.
- lstrip([chars]): Trims characters from the left side of a string. When chars is omitted or None, returns a new string with all leading whitespace cleared.
Read Also: How to Compare String in Python? [With Examples]
Trimming Whitespace from a String Using Strip Methods
The following instance demonstrates how to trim trailing spaces, leading spaces, and both leading and trailing spaces from a string:
s1 = ' shark '
print(f"string: '{s1}'")
s1_remove_leading = s1.lstrip()
print(f"remove leading: '{s1_remove_leading}'")
s1_remove_trailing = s1.rstrip()
print(f"remove trailing: '{s1_remove_trailing}'")
s1_remove_both = s1.strip()
print(f"remove both: '{s1_remove_both}'")
The output is:
string: ' shark '
remove leading: 'shark '
remove trailing: ' shark'
remove both: 'shark'
The following instance demonstrates how to use the same strip methods to trim numerous whitespace characters from a string:
s2 = ' \n shark\n squid\t '
print(f"string: '{s2}'")
s2_remove_leading = s2.lstrip()
print(f"remove leading: '{s2_remove_leading}'")
s2_remove_trailing = s2.rstrip()
print(f"remove trailing: '{s2_remove_trailing}'")
s2_remove_both = s2.strip()
print(f"remove both: '{s2_remove_both}'")
The output is:
Output
string: '
shark
squid '
remove leading: 'shark
squid '
remove trailing: '
shark
squid'
remove both: 'shark
squid'
The output indicates that using the strip methods with the chars statement omitted removes only the leading and trailing space, newline, and tab characters from the string. Any whitespace that’s not at the very start or end of the string isn’t removed.
Read Also: How to Choose the Right Python Development Company?
Trimming a Specific Whitespace Character from a String Using Strip Methods
You can even remove only a character or characters from the start and end of a string by determining the chars argument. The following instance demonstrates how to trim only the leading newline character from a string:
s3 = '\n sammy\n shark\t '
print(f"string: '{s3}'")
s3_remove_leading_newline = s3.lstrip('\n')
print(f"remove only leading newline: '{s3_remove_leading_newline}'")
The output is:
Output
string: '
sammy
shark '
remove only leading newline: ' sammy
shark
The output indicates that the lstrip() method removes the leading newline character but doesn’t clear the leading spaces from the string.
Note that the strip procedure only removes specific characters when they’re the outermost leading and trailing characters. For instance, you can’t use rstrip() to remove only the trailing tab character from s3 = ‘\n sammy\n shark\t ‘ because of the spaces after \t.
Conclusion
In this article, you operated the strip(), rstrip(), and lstrip() techniques to trim leading and trailing whitespace from strings.
Leave a Reply