Remove \x00 from a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 10, 2022

banner

Check out my new book

Remove \x00 from a string in Python #

Use the str.replace() method to remove \x00 from a string, e.g. result = my_str.replace('\x00', ''). The str.replace() method will remove occurrences of the \x00 character by replacing them with an empty string.

main.py
my_str = 'Apple\x00\x00\x00\x00\x00' # ✅ remove \x00 from string using str.replace() result = my_str.replace('\x00', '') print(result) # 👉️ 'Apple' print(repr(result)) # 👉️ 'Apple' # ---------------------------------------- # ✅ remove \x00 from string using str.rstrip() result = my_str.rstrip('\x00') print(result) # 👉️ 'Apple' print(repr(result)) # 👉️ 'Apple' # ---------------------------------------- # ✅ remove \x00 from list of strings my_list = ['apple\x00', 'banana\x00'] result = [string.replace('\x00', '') for string in my_list] print(result) # 👉️ ['apple', 'banana']

The \x00 character is a Null-character that represents a HEX byte with all bits at 0.

The first example uses the str.replace() method to replace all occurrences of the character with an empty string.

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)

Note that the method doesn't change the original string. Strings are immutable in Python.

If the \x00 characters are only at the end of the string, you can also use the str.rstrip() method.

main.py
my_str = 'Apple\x00\x00\x00\x00\x00' result = my_str.rstrip('\x00') print(result) # 👉️ 'Apple' print(repr(result)) # 👉️ 'Apple'

The str.rstrip method returns a copy of the string with the trailing characters removed.

We passed the \x00 character to the method, so all occurrences of \x00 characters at the end of the string will get removed.

Note that this only works if all of the \x00 characters are located at the end of the string. In all other cases, use the str.replace() method.

Remove \x00 from a list of strings in Python #

To remove \x00 characters from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. On each iteration, use the str.replace() method to remove occurrences of \x00.
  3. The strings in the new list won't contain any \x00 characters.
main.py
my_list = ['apple\x00', 'banana\x00'] result = [string.replace('\x00', '') for string in my_list] print(result) # 👉️ ['apple', 'banana']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we replace occurrences of the \x00 character with an empty string and return the result.

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.