How to strip whitespace in Python

Share

In programming, whitespaces refer to any characters used for formatting or spacing purposes but not visible on the screen. They are commonly used to separate words or elements within a line of code or make it more readable.

The most common whitespaces are spaces, tabs, and line breaks. Spaces separate words or elements within a line of code, while tabs are used to indent code to show hierarchy or structure. Line breaks are used to separate lines of code.

In programming languages, such as Python, the use of whitespaces is significant because it determines the structure of the code. For example, indentation indicates the beginning and end of a code block in Python rather than curly braces, as in other languages.

Strip whitespace in Python

To remove whitespaces in Python, we can use the strip(), rstrip(), and lstrip() methods. We will discuss them one by one.

Using the strip()method

The strip() method removes leading and trailing whitespace characters (spaces, tabs, and newline characters) from a string. It delivers an output of a new string with no whitespace character, but the original is not modified.

Example

# Define a string with leading and trailing whitespace
text = " Hello, the string has whitespaces "
# Use strip() to remove the whitespace
stripped_text = text.strip()
# Print the original string and the stripped string to compare them
print("Original string: {text}".format(text= text))
print("Stripped string: {stripped_text}".format(stripped_text= stripped_text))

Explanation

  • Line 2: We define a text with a string having whitespaces at the front and at the end.

  • Line 5: Using the strip() method, we remove the whitespace and save the result in a new variable stripped_text.

  • Line 8: We print the original string to the console.

  • Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters.

Using the rstrip()method

With the help of the rstrip() method, we can easily remove the trailing whitespace characters (spaces, tabs, and newline characters) from a string. It returns an output of a new string with no whitespace character, but the original text is not modified which is similar to strip() method.

Example

# Define a string with leading and trailing whitespace
text = "Hello, whitespaces at the end "
# Use strip() to remove the whitespace
rstrip_text = text.rstrip()
# Print the original string and the stripped string to compare them
print("Original string: {text}".format(text= text))
print("Stripped string: {rstrip_text}".format(rstrip_text= rstrip_text))

Explanation

  • Line 2: We define a text with a string having whitespaces at the end.

  • Line 5: Using the rstrip() method, we remove the whitespace and save the result in a new variable rstrip_text.

  • Line 8: We print the original string to the console.

  • Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters at the end.

Using the lstrip()method

Similarly, the lstrip() method removes only the leading whitespace characters from a string. n is similar to the strip() method. It returns an output of a new string with no whitespace character, but the original text is not modified which is similar to strip() method.

Example

# Define a string with leading and trailing whitespace
text = " Hello, whitespaces at the beginning!"
# Use strip() to remove the whitespace
lstrip_text = text.lstrip()
# Print the original string and the stripped string to compare them
print("Original string: {text}".format(text= text))
print("Stripped string: {lstrip_text}".format(lstrip_text= lstrip_text))

Explanation

  • Line 2: We define a text with a string having whitespaces at the beginning.

  • Line 5: Using the lstrip() method, we remove the whitespace and save the result in a new variable lstrip_text.

  • Line 8: We print the original string to the console.

  • Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters.

Conclusion

It is important to note that while whitespaces are not visible on the screen, they can affect how the computer interprets and executes code. Therefore, it's essential to use them appropriately to ensure the code is correct and functional.

Copyright ©2024 Educative, Inc. All rights reserved