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
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
Common string operations โ€” Python 3.14.3 documentation
It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification". Any other appearance of $ in the string will result in a ValueError being raised. The string module provides a Template class that implements these rules.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.3 ...
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
๐ŸŒ
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
In this lab, you learned how to define special characters in Python. This involved importing the string module and utilizing the string.punctuation constant to access a predefined string of common punctuation characters.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_escape_characters.asp
Python Escape Characters
Built-in Modules Random Module ... Certificate Python Training ... To insert characters that are illegal in a string, use an escape character....
๐ŸŒ
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 `strip()` method is perfect for cleaning up the beginning and end of strings. When you need more control over character removal, regular expressions are your friend. Hereโ€™s a practical example: import re def clean_text(text): # Removes all special characters except spaces and alphanumeric characters cleaned = re.sub(r'[^a-zA-Z0-9\s]', '', text) return cleaned # Real-world example: Cleaning a product description product_desc = "Latest iPhone 13 Pro (128GB) - $999.99 *Limited Time Offer!*" clean_desc = clean_text(product_desc) print(clean_desc) # Output: "Latest iPhone 13 Pro 128GB 999.99 Limited Time Offer"
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-removing-unwanted-characters-from-string
Remove Special Characters from String in Python - GeeksforGeeks
July 11, 2025 - When working with text data in Python, it's common to encounter strings containing unwanted special characters such as punctuation, symbols or other non-alphanumeric elements. For example, given the input "Data!@Science#Rocks123", the desired output is "DataScienceRocks123". Let's explore different methods to achieve this. re.sub() function from re module allows you to substitute parts of a string based on a regex pattern.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ program-to-check-if-a-string-contains-any-special-character
Program to check if a string contains any special character
import re s = "Hello" def has_special_char(s): pattern = r'[^a-zA-Z0-9\s]' output = bool(re.search(pattern, s)) if output == True: print(s, "has the special characters in it") else: print(s, "has no special characters in it") has_special_char(s) ... The string module in Python provides constants containing sets of characters, such as string.punctuation which contains all the ASCII punctuation characters.
๐ŸŒ
Profound Academy
profound.academy โ€บ python-introduction โ€บ special-characters-I2p6JZked4TCREaHyQs4
Special characters - Introduction to Python
November 1, 2024 - This can be a problem as python expects a single quote at the end of the string, not in the middle. There are several options to avoid this issue: We can avoid starting the string with a single quote and use " instead. 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.
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 2.0 โ€บ ref โ€บ strings.html
2.4.1 String literals
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 value string literal (even a raw string cannot end in an odd number of backslashes).
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-print-string-with-special-characters
Print a String with the Special Characters in Python | bobbyhadz
The str.encode() method returns an encoded version of the string as a bytes object. We used the unicode_escape encoding to escape the special characters with an extra backslash.
๐ŸŒ
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 - How to handle accented & special character strings in Python. String operations on string array containing strings with accented &/or special characters alongside regular ascii strings can be quiet โ€ฆ
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-check-for-special-characters-in-a-string-with-python
5 Best Ways to Check for Special Characters in a String with Python โ€“ Be on the Right Side of Change
This snippet uses the string method ... invert the result for our special character check. The string module in Python contains a punctuation attribute that has all the special characters predefined....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string
Python String - GeeksforGeeks
Python does not have a separate character type. A single character is treated as a string of length one.
Published ย  January 24, 2018
๐ŸŒ
Studytonight
studytonight.com โ€บ python-programs โ€บ python-program-to-check-special-characters
Python Program to check special characters - Studytonight
Python program to check if a string has special character or not using search() and compile() of the re module. Special character are symbols and punctuations.
๐ŸŒ
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.