1

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"
Ugohihi
  • 37
  • 11
  • What does `which python3` tell you? – cs95 Jan 21 '18 at 01:45
  • 2
    What do you mean by "the python bash"? – BrenBarn Jan 21 '18 at 01:47
  • 1
    Note that if you just put that code in a file and run it, nothing will show because you're not producing any output. Did you try `print(f"{a} I'm {b} years old")`? – BrenBarn Jan 21 '18 at 01:47
  • /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 – Ugohihi Jan 21 '18 at 01:47
  • Okay well it seems I had not understood it that way, I actually didn't think about it, thanks and sorry for the dumb question – Ugohihi Jan 21 '18 at 01:50
  • 1
    I think what you're calling "Python bash" is actually Python's interactive mode, or REPL (read, evaluate, print loop) -- this has nothing to do with bash, which is a completely different command interpreter. – Gordon Davisson Jan 21 '18 at 02:40

1 Answers1

4

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")
wim
  • 338,267
  • 99
  • 616
  • 750