DICTIONARY KEYERROR :how to use backslash as a key in python dictionary

Viewed 280
syntaxes={
        '>':"greater than",
        "<": "less than",
        "*": "multiplication",
        "+": "addition",
        '/': "forward slash"
        '\': "back slash" # String literal is unterminatedPylance

    }
    
input='>'
print(syntaxes[input])

I am trying to use the back slash as a key "" in python ,but it is not letting me and throwing error

1 Answers
You need to use "\\" double backslash everywhere where ever you need only a single backslash.
In dictionary key as well as while providing input.

syntaxes={
        '>':"greater than",
        "<": "less than",
        "*": "multiplication",
        "+": "addition",
        '/': "forward slash",
        '\\': "back slash" # String literal is unterminatedPylance

    }
input='\\'
print(syntaxes[input])
Related