Search code examples
pythonpython-2.7python-requestspython-importpython-3.7

Why do I receive syntax error while printing format


Person = { 'name' : " Jehn", ' age':23}
Sentence= f' My name is {person ['name']} and i am {person['age']} years old.'

Solution

  • You can change the type of quoting in the format string so it is wrapped with double-quotes like this:

    person = { 'name' : " Jehn", 'age':23} 
    sentence = f" My name is {person ['name']} and i am {person['age']} years old."
    

    Note that there was also space in ' age' which could be missed easily. Also, I changed the variables to be all lowercase letters to be more Pythonic and consistent.