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
🌐
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
🌐
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.
Find elsewhere
🌐
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.
🌐
Medium
medium.com › @ryan_forrester_ › remove-special-characters-from-strings-in-python-complete-guide-53651c8163d9
Remove Special Characters from Strings in Python: Complete Guide | by ryan | Medium
January 7, 2025 - The simplest way to remove specific special characters is with Python’s built-in string methods. Here’s how they work: # Using replace() to remove specific characters text = "Hello!
🌐
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.
🌐
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.
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-a-string-contains-special-characters-in-python-559570
How to Check If a String Contains Special Characters in Python | LabEx
Learn how to check if a string contains special characters in Python. Use regular expressions and str.isalnum() to identify special characters in Python strings. Python string special character check tutorial.
🌐
W3Schools
w3schools.com › python › gloss_python_escape_characters.asp
Python Escape Characters
Python Examples Python Compiler ... Bootcamp Python Training ... To insert characters that are illegal in a string, use an escape character....
🌐
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.
🌐
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".