Python Forum
I am getting KeyError with format command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting KeyError with format command
#1
print("{'1','2','3'} type is {}".format(type({'1', '2', '3'})))

This line throws KeyError.

File "c:/ex2_type.py", line 12, in main
print("\{'1','2','3'\} type is {}".format(type({'1', '2', '3'})))
KeyError: "'1','2','3'\\"

I want to use the format command to print the desired output. What needs to be changed?
Reply
#2
>>> print(f'{"key":1} is {type({"key":1})}')
Output:
key is <class 'dict'>
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
These work.
print('{} type is {}'.format("{'1','2','3'}", type({'1', '2', '3'})))
print("{{'1','2','3'}} type is {}".format(type({'1','2','3'})))
print(f"{{'1','2','3'}} type is {type({'1','2','3'})}")
Output:
{'1','2','3'} type is <class 'set'> {'1','2','3'} type is <class 'set'> {'1','2','3'} type is <class 'set'>
Reply
#4
I appreciate your responses. It worked!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020