Python will check the first or second line for an emacs/vim-like encoding specification.

More precisely, the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)". The first group of this expression is then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation.

Source: PEP 263

(A BOM would also make Python interpret the source as UTF-8.

I would recommend, you use this over .decode('utf8')

# -*- encoding: utf-8 -*-
special_char_string = u"äöüáèô"

In any case, special_char_string will then contain a unicode object, no longer a str. As you can see, they're both semantically equivalent:

>>> u"äöüáèô" == "äöüáèô".decode('utf8')
True

And the reverse:

>>> u"äöüáèô".encode('utf8')
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'
>>> "äöüáèô"
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'

There is a technical difference, however: if you use u"something", it will instruct the parser that there is a unicode literal, it should be a bit faster.

Answer from phant0m on Stack Overflow
🌐
Leyaa
leyaa.ai › home › how to handle special characters data in python correctly
How to Handle Special Characters Data in Python Correctly | Leyaa.ai
March 9, 2026 - In Python, handle special characters by using UTF-8 encoding when reading or writing data and by using Unicode strings (default in Python 3). Always specify encoding explicitly in file operations to avoid errors with special characters.
🌐
Madhur
madhur.co.in › blog › 2016 › 04 › 25 › handling-special-characters-python.html
Handling special characters in Python 2 – Madhur Ahuja
SyntaxError: Non-ASCII character '\xe2' in file pychar.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details · As the error suggests, the first step is to declare encoding on top of file: ... Now, the program will happily print out the string in the output. Apart from that, it is also helpful to know that in python, these utf8 characters can be represented in both encoded and decoded forms.
🌐
Medium
randombites.medium.com › how-to-handle-accented-special-strings-175e65d96123
How to handle accented & special character strings in Python. | by MP | Medium
September 14, 2018 - Strings with accented or special characters are unicode strings while regular one’s ascii. So to handle unicode strings as regular ascii strings one has to convert unicode strings to ascii.
🌐
Profound Academy
profound.academy › python-introduction › special-characters-I2p6JZked4TCREaHyQs4
Special characters • Introduction to Python
November 1, 2024 - Yet, this is not a great solution as the string might contain a symbol " as well (A really “good” product). We can use an escape character \ which is specifically designed for these situations.
🌐
CodeSignal
codesignal.com › learn › courses › string-manipulation-for-python-coders › lessons › unleashing-python-strings-a-beginners-guide-to-escaping-and-special-characters
A Beginner's Guide to Escaping and Special Characters
By incorporating special characters in strings, you essentially enhance their expressiveness. Let's create a string, for instance, that represents a simple table: table = "Name\tAge\nAlice\t23\nBob\t25" print(table) # Outputs data like a table """ Name Age Alice 23 Bob 25 """ You can also include internal quotes by using different types for your string and your internal quotes: single_quoted = 'This is a "quoted" word' # using single quotes to print double quotes inside the string print(single_quoted) # This is a "quoted" word double_quoted = "This is a 'quoted' word" # using double quotes to print single quotes inside the string print(double_quoted) # This is a 'quoted' word
Find elsewhere
🌐
CodeRivers
coderivers.org › blog › how-to-keep-special-characters-in-between-characters-python
Keeping Special Characters in Between Characters in Python - CodeRivers
February 3, 2025 - Here, we first create a list of individual characters from the string using a list comprehension. Then we use the join method of the special_char to join all the elements of the list together, with the special_char in between each element. Python's string formatting can also be used to insert special characters.
🌐
CodeGenes
codegenes.net › blog › how-to-keep-special-characters-in-between-characters-python
Preserving Special Characters in Between Characters in Python — codegenes.net
When working with files or external data sources, it's recommended to use UTF - 8 encoding explicitly. This ensures that special characters from different languages and character sets are handled correctly.
🌐
InterServer
interserver.net › home › python › how to use escape characters in python strings effectively
How to Use Escape Characters in Python Strings Effectively - Interserver Tips
April 23, 2026 - Escape characters help you include special symbols in strings without breaking the code. They use a backslash (\) followed by another character to tell Python what to do. For example, \n adds a new line, and \t adds a tab space. Using them correctly makes your code cleaner and easier to read.
🌐
W3Schools
w3schools.com › python › gloss_python_escape_characters.asp
Python Escape Characters
Python Examples Python Compiler ... Q&A Python Training ... To insert characters that are illegal in a string, use an escape character....
🌐
Medium
mike-vincent.medium.com › quarks-outlines-python-special-characters-21e932f54d0e
Quark’s Outlines: Python Special Characters | by Mike Vincent | Medium
May 19, 2025 - Special characters help Python know where strings, comments, and line breaks are. These examples show how to use them clearly and avoid common mistakes. Problem: You need to write the sentence He said, "Yes" as a string. But quotes inside quotes can confuse Python.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › python-program-check-string-contains-special-character
Program to check if a string contains any special character - GeeksforGeeks
April 8, 2024 - If a character is neither an alphabetical character nor a digit, it is considered a special character. The time complexity of this function is O(n), where n is the length of the input string, because it involves a single loop that iterates through ...
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node14.html
Special Characters and Raw Strings
Inside of any of the pairs of quotes described in the previous section, there are special character sequences, beginning with a backslash (\), which are interpreted in a special way. Table 2.1 lists these characters. Using a single backslash as a continuation character is an alternative to using triple quoted strings when you are construction a string constant.
Top answer
1 of 3
13

Prevention is better than cure. What you need is to find out how that rubbish is being created. Please edit your question to show the code that creates it, and then we can help you fix it. It looks like somebody has done:

your_unicode_string =  original_utf8_encoded_bytestring.decode('latin1')

The cure is to reverse the process, simply, and then decode.

correct_unicode_string = your_unicode_string.encode('latin1').decode('utf8')

Update Based on the code that you supplied, the probable cause is that the website declares that it is encoded in ISO-8859-1 (aka latin1) but in reality it is encoded in UTF-8. Please update your question to show us the url.

If you can't show it, read the BS docs; it looks like you'll need to use:

BeautifulSoup(web, from_encoding='utf8')
2 of 3
3

Unicode support in many languages is confusing, so your error here is understandable. Those strings are UTF-8 bytes, which would work properly if you drop the u at the front:

>>> err = u'\xc3\x96berg'
>>> print err
Ã?berg
>>> x = '\xc3\x96berg'
>>> print x
Öberg
>>> u = x.decode('utf-8')
>>> u
u'\xd6berg'
>>> print u
Öberg

For lots more information:

http://www.joelonsoftware.com/articles/Unicode.html

http://docs.python.org/howto/unicode.html


You should really really read those links and understand what is going on before proceeding. If, however, you absolutely need to have something that works today, you can use this horrible hack that I am embarrassed to post publicly:

def convert_fake_unicode_to_real_unicode(string):
    return ''.join(map(chr, map(ord, string))).decode('utf-8')
🌐
Grokbase
grokbase.com › t › python › python-list › 131146a7y5 › handling-special-characters-in-python
[Python] Handling Special characters in python - Grokbase
January 1, 2013 - Unicode, in particular, seems to work properly. Thanks Chris Rebert for your suggestion, I tried with PyODBC module, But at the place of "en dash(-)", I am getting '?' symbol. How can I overcome this. ... On Jan 1, 2013 3:41 AM, wrote: I am facing one issue in my module. I am gathering data from sql server database. In the data that I got from db contains special characters like "endash". Python was taking it as "\x96".