Remove \xa0 from a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 10, 2022

banner

Photo from Unsplash

Remove \xa0 from a string in Python #

Use the unicodedata.normalize() method to remove \xa0 from a string, e.g. result = unicodedata.normalize('NFKD', my_str). The unicodedata.normalize method returns the normal form for the provided unicode string by replacing all compatibility characters with their equivalents.

main.py
import unicodedata my_str = 'hello\xa0world' # ✅ remove \xa0 from string using unicodedata.normalize() result = unicodedata.normalize('NFKD', my_str) print(result) # 👉️ 'hello world' # ---------------------------------------- # ✅ remove \xa0 from string using str.replace() result = my_str.replace('\xa0', ' ') print(result) # 👉️ 'hello world' # ---------------------------------------- # ✅ remove \xa0 from list of strings my_list = ['hello\xa0', '\xa0world'] result = [string.replace('\xa0', ' ') for string in my_list] print(result) # 👉️ ['hello ', ' world']
The \xa0 character represents non-breaking space, so the way to remove it from a string is to replace it with a space.

The unicodedata.normalize method returns the normal form for the provided Unicode string.

The first argument is the form - NFKD in our case. The normal form NFDK replaces all compatibility characters with their equivalents.

Since the equivalent of the \xa0 character is a space, it gets replaced by a space.

If you get unexpected results when using the NFKD form, try using one of NFC, NFKC and NFD.

The NFKC form first applies compatibility decomposition, then canonical decomposition.

main.py
import unicodedata my_str = 'hello\xa0world' result = unicodedata.normalize('NFKC', my_str) print(result) # 👉️ 'hello world'

Alternatively, you can use the str.replace() method.

Use the str.replace() method to remove \xa0 from a string, e.g. result = my_str.replace('\xa0', ' '). The str.replace() method will replace all occurrences of the \xa0 (non-breaking space) character with a space.

main.py
my_str = 'hello\xa0world' result = my_str.replace('\xa0', ' ') print(result) # 👉️ 'hello world'

Since the \xa0 character represents a non-breaking space, we can simply replace it with a space.

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.

Remove \xa0 from a list of strings in Python #

To remove \xa0 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 replace occurrences of \xa0 with a space.
  3. The strings in the new list won't contain any \xa0 characters.
main.py
my_list = ['hello\xa0', '\xa0world'] result = [string.replace('\xa0', ' ') for string in my_list] print(result) # 👉️ ['hello ', ' world']

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 \xa0 character with a space 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.