Search code examples
pythonbashpython-3.6f-string

Python3 f-strings only interpreted in bash


I am currently learning python and have noticed an issue: If I write a .py file and interpret it with the python3 myfile.py command line then the f-string below simply won't show, but if I do the same directly in the python bash it works just fine.

Where could this come from ? (my python version is 3.6.4 (I also have a 2.7 version installed) and I work on macOS)

a = "Hello"
b = 22
f"{a} I'm {b} years old"

Solution

  • That's normal, when executing code in the interactive Python interpreter there is a REPL - read, evaluate, print loop.

    When executing from a .py file, there is no REPL. If you want the string to appear on stdout, you will need to actually print it:

    print(f"{a} I'm {b} years old")