You can fix the backslash by escaping it and ' can be fixed by putting it in double quotes:

symbols = {..., '\\', ... "'", ...}

But typing all this out is pretty tedious. Why not just use string.punctuation instead:

>>> from string import punctuation
>>> set(punctuation)
{'~', ':', "'", '+', '[', '\\', '@', '^', '{', '%', '(', '-', '"', '*', '|', ',', '&', '<', '`', '}', '.', '_', '=', ']', '!', '>', ';', '?', '#', '$', ')', '/'}
>>>
Answer from user2555451 on Stack Overflow
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node14.html
Special Characters and Raw Strings
Next: Unicode Strings Up: String Data Previous: String Constants &nbsp Contents · 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.
Discussions

encoding - Print special characters in list in Python - Stack Overflow
The problem is that Python runs str(my_list) under the hoods, before printing it. And that, in turn, runs repr() on each of the list's elements. Now, repr() on a string returns an ASCII-only representation of the string. That is, those '\xc3' you're seeing are an actual backslash, an actual 'c' and an actual '3' characters... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between special and escape characters?
An escape character is a character to indicate that whatever follows it should be interpreted in a special way. The escape character for python strings is \, for printf it's % and for format it's { and }. For example: data = 'one line \n next line' The \ tells python that the n following it is not a real, literal character 'n', but instead is a newline. There is no universal definition of the term "special character". I'm assuming your professor means the the characters that the escape characters encode for. In my above example, python replaces the code \n with the non printable newline character. More on reddit.com
🌐 r/learnpython
4
1
September 21, 2020
How to make special characters to string?
Python's strings use Unicode, so they should be able to represent text in whatever language you desire. This is probably a codec issue, in which case I would suggest trying UTF-8 instead of whatever the default is on your system. But that's only speculation; it would really help us to see some examples of code and any errors you get. More on reddit.com
🌐 r/learnpython
2
1
November 28, 2023
Let python ignore special characters in a string.

Your while loop is unnecessary, and so is the i variable. If you change giggity to word, and outdent the return statement to the first indentation level of the function, it should work.

Alternatively, a more pythonic solution would be

from string import ascii_lowercase
def remove_extra(word):
    return ''.join(c for c in word.lower() if c in ascii_lowercase)
More on reddit.com
🌐 r/learnpython
13
9
March 7, 2016
🌐
Profound Academy
profound.academy › python-introduction › special-characters-I2p6JZked4TCREaHyQs4
Special characters • Introduction to Python
November 1, 2024 - \ escapes any character in the string that comes right after it. Our string declaration would become a = 'Hi, I\'m a programmer'. This tells Python to treat the middle ' as a simple symbol, not an end of a string. Here are several popular special characters in 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.
🌐
DEV Community
dev.to › mike-vincent › quarks-outlines-python-special-characters-3cf
Quark’s Outlines: Python Special Characters - DEV Community
May 19, 2025 - 1991 — Comment with hash — Python 0.9.0 used # for comments and \ for line continuation. 2000 — Raw strings — Python 2.0 added r"..." to show that backslashes are literal. 2008 — Triple quotes — Python 3 formalized ''' and """ for multi-line strings. How do you use Python’s special characters the right way?
Find elsewhere
🌐
W3Schools
w3schools.com › python › gloss_python_escape_characters.asp
Python Escape Characters
Remove List Duplicates Reverse ... Bootcamp Python Training ... To insert characters that are illegal in a string, use an escape character....
🌐
w3resource
w3resource.com › python-exercises › python-basic-exercise-92.php
Python: Define a string containing special characters in various forms - w3resource
May 17, 2025 - # Print a string containing special characters without escaping. print() print("\#{'}${\"}@/") # Print a string containing special characters with escaped single quotes. print("\#{'}${\"}@/") # Print a raw string using triple-quotes with special characters. print(r"""\#{'}${"}@/""") # Print a string containing special characters without escaping. print('\#{\'}${"}@/') # Print a string containing special characters with escaped single quotes. print('\#{'"'"'}${"}@/') # Print a raw string using triple-quotes with special characters. print(r'''\#{'}${"}@/''') print() ... Write a Python program to check if a string contains special characters.
🌐
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
For example, '\"' is used in Python to denote a quotation mark inside a string, as demonstrated below: greeting = "The machine said: \"Hello, World!\"" print(greeting) # The machine said: "Hello, World!" ... Special characters such as newline (\n), tab (\t), and the backslash itself (\\), impart ...
🌐
Scaler
scaler.com › home › topics › remove special characters from string python
Remove Special Characters From String Python - Scaler Topics
January 6, 2024 - The string.isalnum() method returns True if all the characters in the string are alphabets or numbers and returns False if it finds any special character in the string. We can use this property to remove all special characters from a string in python. The following code illustrates this, The ...
🌐
freeCodeCamp
freecodecamp.org › news › escape-sequences-python
Escape Sequences in Python
January 3, 2020 - File "main.py", line 1 print(r"There's an unescaped backslash at the end of this string\") ^ SyntaxError: EOL while scanning string literal · A full list of escape sequences can be found here in the Python docs.
🌐
Scaler
scaler.com › home › topics › how to escape characters in a python string?
How to Escape Characters in a Python String? - Scaler Topics
April 25, 2024 - Another way to escape string in Python is by using a translate table that returns a translated string based on the character translations that we define inside the table. The table comprises an object (dict objects) where each of the character definitions is mapped to their character translations. Therefore, the special characters in the entire string will be converted to their respective character changes given in the table.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-escape-reserved-characters-in-strings-list
Python - Escape reserved characters in Strings List - GeeksforGeeks
March 24, 2023 - Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list. Method #2 : Using maketrans() + translate() + zip() In this, the escaping is made by pairing using zip() and maketrans() rather than dictionary for mapping. The translation is done using the result of maketrans(). ... # Python3 code to demonstrate working of # Escape reserved characters in Strings List # Using maketrans() + translate() + zip() # initializing list test_list = ["Gf-g", "is*", "be)s(t"] # printing string print("The original list : " + str(test_list)) # r
Top answer
1 of 2
3

If possible, switch to Python 3 and you'll get the expected result.

If you have to make it work in Python 2, then use unicode strings:

my_list = [u'éléphant', u'Hello World']

The way you have it right now, Python is interpreting the first string as a series of bytes with values '\xc3\xa9l\xc3\xa9phant' which will only be converted to Unicode code points after properly UTF-8 decoded: '\xc3\xa9l\xc3\xa9phant'.decode('utf8') == u'\xe9l\xe9phant'.

If you wish to print list repr and get "unicode" out, you'll have to manually encode it as UTF-8 (if that's what your terminal understands).

>>> print repr(my_list).decode('unicode-escape').encode('utf8')
[u'éléphant', u'Hello World']

But it's easier to format it manually:

>>> print ", ".join(my_list)
éléphant, Hello World
2 of 2
3

Short answer, you have to implement it yourself, if you want to keep the output in that format:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

my_list = ['éléphant', 'Hello World']

def print_list (l):
    print ("[" + ", ".join(["'%s'" % str(x) for x in l]) + "]")

print_list (my_list)

Which generates the expected

['éléphant', 'Hello World']

However, note that it would put all elements inside quotes (even numbers, for example), so you may need a more complex implementation, if you're expecting anything other than strings on your list.

Longer answer

The problem is that Python runs str(my_list) under the hoods, before printing it. And that, in turn, runs repr() on each of the list's elements.

Now, repr() on a string returns an ASCII-only representation of the string. That is, those '\xc3' you're seeing are an actual backslash, an actual 'c' and an actual '3' characters.

You can't work around that, as the problem is on the implementation of list.__str__ ().

Below, a sample program to demonstrate that.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# vi: ai sts=4 sw=4 et

import pprint

my_list = ['éléphant', 'Hello World']

# under the hood, python first runs str(my_list), before printing it
my_list_as_string = str(my_list)

# str() on a list runs repr() on each of the elements.
# However, it seems that __repr__ on a string transforms it to an 
# ASCII-only representation
print ('str(my_list) = %s' % str(my_list))
for c in my_list_as_string:
    print c
print ('len(str(my_list)) = %s' % len(str(my_list)))
print ("\n")

# Which we can confirm here, where we can see that it it also adds the quotes:
print ('repr("é") == %s' % repr("é"))
for c in repr("é"):
    print c
print ('len(repr("é")) == %s' % len(repr("é")))
print ("\n")

# Even pprint fails
print ("pprint gives the same results")
pprint.pprint(my_list)

# It's useless to try to encode it, since all data is ASCII
print "Trying to encode"
print (my_list_as_string.encode ("utf8"))

Which generates this:

str(my_list) = ['\xc3\xa9l\xc3\xa9phant', 'Hello World']
[
'
\
x
c
3
\
x
a
9
l
\
x
c
3
\
x
a
9
p
h
a
n
t
'
,

'
H
e
l
l
o

W
o
r
l
d
'
]
len(str(my_list)) = 41


repr("é") == '\xc3\xa9'
'
\
x
c
3
\
x
a
9
'
len(repr("é")) == 10


pprint gives the same results
['\xc3\xa9l\xc3\xa9phant', 'Hello World']
Trying to encode
['\xc3\xa9l\xc3\xa9phant', 'Hello World']
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 14_regex › spec_sym.html
Special symbols - Python for network engineers
Expression [a-f0-9]+\.[a-f0-9]+\.[a-f0-9]+ describes three groups of symbols separated by a dot. Characters in each group can be letters a-f or digits 0-9. This expression describes MAC address. Another feature of square brackets is that the special symbols within square brackets lose their special meaning and are simply a symbol.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › escapes.html
Escape Characters — Python Reference (The Right Way) 0.1 documentation
For example, the string literal r”n” consists of two characters: a backslash and a lowercase ‘n’. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r”“” is a valid string literal consisting of two characters: a backslash and a double quote; r”” is not a valid string literal (even a raw string cannot end in an odd number of backslashes).
🌐
Sololearn
sololearn.com › en › Discuss › 2408789 › how-to-check-whether-the-special-characters-are-present-in-a-string
How to check whether the special characters are present in a string? | Sololearn: Learn to code for FREE!
December 7, 2023 - Read that tutorial 👇👇 https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2476/ ... ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ But isalpha, isnum is for checking the letters and numbers, not special characters. ... I don't know why people think regex is hard, but I think it becomes ridiculously simple once you get the concept. By the way, here's the code: import re string = "jhgr#66£" ptrn = re.compile(r"[@#$!%&*]") print(bool(ptrn.search(string))) https://code.sololearn.com/ctmbRc843HKF/?ref=app