How To Trim Whitespace from a String in Python

Introduction

Python provides three methods that you can use to trim whitespace from a string and return a new string object. The string strip methods can trim leading whitespace, trailing whitespace, or both. To learn more about removing whitespace, including how to remove all whitespace or only duplicate spaces, refer to How To Remove Spaces from a String In Python.

Whitespace includes all Unicode whitespace characters, such as spaces, tabs (\t), carriage returns (\r), and newlines (\n). The Python str() class has the following methods that you can use to trim whitespace from a string:

  • strip([chars]): Trims characters from both ends of a string. When chars is omitted or None, returns a new string with all leading and trailing whitespace removed.
  • 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 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 removed.

Trimming Whitespace from a String Using Strip Methods

The following example demonstrates how to trim leading spaces, trailing 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 example demonstrates how to use the same strip methods to trim multiple 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 shows that using the strip methods with the chars argument omitted removes only the leading and trailing space, newline, and tab characters from the string. Any whitespace that’s not at the very beginning or end of the string isn’t removed.

Trimming a Specific Whitespace Character from a String Using Strip Methods

You can also remove only a character or characters from the beginning and end of a string by specifying the chars argument. The following example 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 shows that the lstrip() method removes the leading newline character but doesn’t remove the leading spaces from the string.

Note that the strip method only removes specific characters when they’re the outermost leading and trailing characters. For example, 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 used the strip(), rstrip(), and lstrip() methods to trim leading and trailing whitespace from strings. To learn how to remove spaces and characters from within strings, refer to How To Remove Spaces from a String In Python. Continue your learning with more Python string tutorials.

Source:
https://www.digitalocean.com/community/tutorials/python-trim-string-rstrip-lstrip-strip