Strip whitespace from Bytes in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Strip whitespace from Bytes in Python #

To strip the whitespace from a bytes object:

  1. Use the bytes.decode() method to convert the bytes object to a string.
  2. Use the str.strip() method to remove all leading and trailing whitespace from the string.
  3. Use the str.encode() method to convert the string to a bytes object.
main.py
my_str = ' hello world ' print(type(my_str)) # 👉️ <class 'str'> my_bytes = my_str.encode('utf-8') my_str = my_bytes.decode('utf-8') print(repr(my_str)) # 👉️ ' hello world ' # ✅ Strip leading and trailing whitespace result = my_str.strip() print(repr(result)) # 👉️ 'hello world' # ✅ Or remove all whitespace result = my_str.replace(' ', '') print(repr(result)) # 👉️ 'helloworld' # ✅ Convert string back to bytes object if necessary my_bytes = result.encode('utf-8') print(my_bytes) # 👉️ b'helloworld'

The first step is to use the bytes.decode() method to convert the bytes object to a string.

The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8.

main.py
my_str = ' hello world ' print(type(my_str)) # 👉️ <class 'str'> my_bytes = my_str.encode('utf-8') my_str = my_bytes.decode('utf-8') print(repr(my_str)) # 👉️ ' hello world ' result = my_str.strip() print(repr(result)) # 👉️ 'hello world'

We can use the str.strip() method to remove all leading and trailing whitespace from the string.

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

If you need to remove all spaces from the string, use the str.replace() method instead.

main.py
my_str = ' hello world ' print(type(my_str)) # 👉️ <class 'str'> my_bytes = my_str.encode('utf-8') my_str = my_bytes.decode('utf-8') print(repr(my_str)) # 👉️ ' hello world ' result = my_str.replace(' ', '') print(repr(result)) # 👉️ 'helloworld'

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
We remove all spaces from the string by replacing each space with an empty string.

If you need to convert the string back to a bytes object, use the str.encode() method.

main.py
my_str = ' hello world ' print(type(my_str)) # 👉️ <class 'str'> my_bytes = my_str.encode('utf-8') my_str = my_bytes.decode('utf-8') print(repr(my_str)) # 👉️ ' hello world ' result = my_str.replace(' ', '') print(repr(result)) # 👉️ 'helloworld' my_bytes = result.encode('utf-8') print(my_bytes) # 👉️ b'helloworld'

The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.