I can't make a dictionary that has # symbol inside the key

3506 views python

I wanted to make a dictionary that looks like this:

example = dict(C# = "o.ooo.")

Because there is '#' symbol, the rest greys out.

I know I can fix this problem by doing this:

test = [("C#"), ("o.ooo.")]
example = dict(test)

I was wondering if there was something that could fix my problem such as:

example = dict(r(C#) = "o.ooo.") - which obviously doesn't work.

answered question

1 Answer


Passing keywords to the dict() built-in function is problematic here, because the # in your key is being misinterpreted as the beginning of a comment.

Instead, use the literal syntax:

example = {"C#": "o.ooo."}

posted this

Have an answer?


Please login first before posting an answer.