Today we will learn how to remove \xa0 from a string in Python. There are many ways to do that, and we will show you three straightforward ways. So read on and choose the one that works for you.
Remove \xa0 from a string in Python
To remove \xa0, we need to know what it is. \xa0 is actually a white space in Latin1 (ISO 8859-1). It is used to avoid line breaks at some position in a string.
Now we have a basic understanding of what it is, let’s start looking for a way to remove it from the string.
Using replace()
function
Syntax:
string.replace(str, replace_str, count)
Parameters:
- str: a given string
- replace_str: represent the string that will replace the given string.
- count (Optional): specify the number of times that you want to replace.
Here, we use the replace()
function to replace \xa0 with white space. Like this:
strMesg = "learn\xa0Python\xa0programming\xa0at:\xa0learnshareit.com" print(strMesg) # same result but not removed \xa0 strMesg = strMesg.replace('\xa0', ' ') # remove \xa0 print(strMesg)
Output:
learn Python programming at: learnshareit.com
learn Python programming at: learnshareit.com
Using normalize()
function in unicodedata library
Syntax:
normalize(form, unicode_str)
Parameters:
- form: a standardized form, the valid value of the form is: ‘NFC’, ‘NFKC’, ‘NFD’, and ‘NFKD’.
- unicode_str: a Unicode string.
To use the normalize()
function, we need to import the unicodedata library with a line of code like this:
import unicodedata
After importing unicodedata, we can use normalize()
. This function will return us a standard string of our original Unicode. Like this:
import unicodedata strMesg = "learn\xa0Python\xa0programming\xa0at:\xa0learnshareit.com" strMesg = unicodedata.normalize("NFKD", strMesg) print(strMesg)
Output:
learn Python programming at: learnshareit.com
Using the join()
function and split()
function.
The split()
function in Python splits a string by the specified delimiter and returns a list of substrings.
split()
syntax:
str.split(separator, num)
Parameters:
- separator: division character to divide elements. The default value whitespace is a separator.
- num (Optional): Specifies how many splits to do.
The join()
function joins all elements in an iterable object together by a specified string.
join()
syntax:
str.join(iterable)
Parameters:
- str: separator character between strings.
- iterable: an iterable object contains elements that are strings to be concatenated.
The default value passed to split() is a space, and \xa0 also represents a space. Therefore, the split() function will split the string into an iterable object with the delimiter \xa0. And we can use the join() function to concatenate the result returned from the split() function and remove \xa0 from a string. Like this:
strMesg = "learn\xa0Python\xa0programming\xa0at:\xa0learnshareit.com" strMesg = ' '.join(strMesg.split()) print(strMesg)
Output:
learn Python programming at: learnshareit.com
Summary
We have just shown you how to remove \xa0 from a string in Python, and that’s it for today. If you still encounter any complex topics when learning Python programming, please comment below! We’ll help you. Have a nice day!
Maybe you are interested:
- Concatenate a String and an Integer in Python
- Convert float to string with N digits precision in Python
- Remove everything after a character in a string in Python

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java