While reviewing a client’s automation script, I ran into an error that read:
SyntaxError: invalid character in identifierAt first glance, it looked confusing because the code seemed fine. However, after digging deeper, I realized the issue was caused by hidden or invalid characters in the script.
If you’ve been coding in Python long enough, you’ll likely face this error too. It usually happens when copying code from websites, emails, or documents, where invisible characters sneak in.
In this tutorial, I’ll share the exact steps I use to fix this error. I’ll also explain why it happens and show you multiple ways to resolve it quickly.
What Does “Invalid Character in Identifier” Mean?
In Python, identifiers (like variable names, function names, or class names) can only contain letters, digits, and underscores. They cannot include spaces, dashes, or hidden characters.
For example:
Valid identifiers:
employee_name = "John"
total_sales_2025 = 500Invalid identifiers:
employee-name = "John" # dash not allowed
total sales = 500 # space not allowedSometimes, the problem is not visible. You might have copied code that contains a non-breaking space (U+00A0) or other Unicode characters that Python doesn’t accept.
Method 1 – Check for Hidden Characters
The first thing I do is check if there are invisible characters in the Python code.
Here’s a quick example. Imagine this code:
salary = 5000
bonuѕ = 2000 # Looks fine, but "s" is actually a Cyrillic character
print(salary + bonuѕ)At first glance, it looks correct. But if you run it, Python throws:
SyntaxError: invalid character in identifierThe issue here is that the second variable uses a Cyrillic small letter es (U+0455) instead of the English s.
How I Fix It:
I retype the variable names manually in my editor. Or, I use a tool to highlight Unicode characters.
Here’s a small script you can use to detect invalid characters:
with open("script.py", "r", encoding="utf-8") as f:
for i, line in enumerate(f, start=1):
for ch in line:
if ord(ch) > 127:
print(f"Line {i}: Invalid character -> {repr(ch)}")You can refer to the screenshot below to see the output:

Run this on your file, and it will tell you exactly where the invalid character is hiding.
Method 2 – Replace Non-Breaking Spaces
Another common cause is non-breaking spaces (U+00A0). These often appear when copying code from websites or Word documents.
Example:
total = 1000 # The space here is not a normal space
print(total)This throws the same error.
Fix:
Replace all non-breaking spaces with normal spaces. In most editors, you can:
- Use Find and Replace (search for \xa0 or paste the offending space).
- In VS Code, enable “Render whitespace” to see hidden spaces.
Or programmatically:
with open("script.py", "r", encoding="utf-8") as f:
content = f.read()
# Replace non-breaking spaces with normal spaces
content = content.replace("\u00A0", " ")
with open("script_fixed.py", "w", encoding="utf-8") as f:
f.write(content)You can refer to the screenshot below to see the output:

Method 3 – Ensure Proper Indentation
Sometimes, the error comes from indentation that uses non-standard spaces.
Example:
def calculate_tax():
print("Calculating tax...") # Indentation uses non-breaking spacesThis looks fine, but Python rejects it.
Fix:
Convert indentation to standard spaces or tabs. Most editors have a “Convert Indentation” option.
In VS Code:
- Select all code → Press Ctrl+Shift+P → Search for Convert Indentation to Spaces.
Method 4 – Avoid Copy-Paste Errors
I’ve seen this error occur frequently when copying code from:
- PDF documents
- Word files
- Websites with styled text
The safest fix is to retype the code manually in your editor.
If you must copy-paste, paste it into a plain-text editor (like Notepad on Windows or TextEdit in plain mode on macOS) before moving it into Python. This strips out hidden characters.
Method 5 – Use a Linter or IDE
Modern IDEs like PyCharm or VS Code can highlight invalid characters instantly.
For example, PyCharm will underline the invalid identifier and show a tooltip like:
“Invalid character in identifier”
This makes it much easier to spot and fix without manually scanning the code.
A Real-World Example
Let’s say I’m working on a payroll script for a U.S. company. I copied a snippet from an email:
employee_name = "James"
houŕly_rate = 25 # Notice the "ŕ" is not a normal "r"
hours_worked = 40
print("Weekly Pay:", houŕly_rate * hours_worked)Running this gives me:
SyntaxError: invalid character in identifierFixed Version:
employee_name = "James"
hourly_rate = 25
hours_worked = 40
print("Weekly Pay:", hourly_rate * hours_worked)Now it runs perfectly:
Weekly Pay: 1000You can refer to the screenshot below to see the output:

Conclusion
- This error usually comes from hidden or invalid characters in variable names or indentation.
- Always check for Unicode characters (like Cyrillic letters or non-breaking spaces).
- Use tools like VS Code, PyCharm, or the detection script I shared to quickly find the problem.
- When in doubt, retype the code manually to ensure it only has valid characters.
I hope you found this tutorial helpful. If you run into this error again, you’ll know exactly where to look and how to fix it.
Other Python tutorials you may also like:
- Print an Array with Commas in Python
- Find the Sum of an Array in Python
- Remove Duplicates from an Array in Python
- Check if an Array Index Exists in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
the solution is just rewrite the code???
Yes, as I said above, this occured for me when I copied some code to test directly on the editor.
The best way is, copy to a notepad and then from notepad you can copy and paste it in the code editor.