Search code examples
pythonformattingcode-cleanup

Why string formatting in python not working if I write f?


name = 'peace'
age = 21
print(f"your name is {name} and your age is {age}")

error File "", line 1 f"your name is {name} and your age is {age}."

SyntaxError: invalid syntax


Solution

  • If your Python version is older than 3.6 you can not use f-strings because they are added in Python 3.6, but you can use "{}".format(something)

    name = 'peace'
    age = 21
    print("your name is {} and your age is {}".format(name,age))
    

    Also see: PEP 3101 -- Advanced String Formatting