Python Forum
F-String not working when in IDLE editor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: F-String not working when in IDLE editor (/thread-15709.html)



F-String not working when in IDLE editor - nadimsarrouh - Jan-28-2019

Hello everyone,

i am a complete python beginner, so I have a very basic question that i couldn't resolve on my own.

I am trying to make use of f-string. I have the newest IDLE version installed on a mac (Python 3.7.2). I wrote the following script, named f.py:
name = "Eric"
age = 74
f"Hello, {name}. You are {age}."
If I run this script I get nothing displayed in the shell, not even an error message. Just the restart message
================ RESTART: /Users/nadim/Documents/python/f.py ================
>>> 
If I however enter these three lines directly in the shell it works like a charm.

>>> name = "Eric"

>>> age = 74

>>> f"Hello, {name}. You are {age}."

'Hello, Eric. You are 74.'
I really would like to use f-strings in my scripts, but can't seem to get it working. Does anybody have an idea?


RE: F-String not working when in IDLE editor - gontajones - Jan-28-2019

print(f"Hello, {name}. You are {age}.")



RE: F-String not working when in IDLE editor - nadimsarrouh - Jan-28-2019

Wow thanks, that worked.

I couldn't find this in any documentation for the f-string. Is there a reason why it doesn't work as described for me without the print function?


RE: F-String not working when in IDLE editor - gontajones - Jan-28-2019

When you're running a script and want to show something in the console you must write this content to stdout/stderr.
The builtin function print() does this for you (writes to stdout by default).
If you do like what you did this content will be lost after the interpreter pass through it.
When running code inside the interpreter it just echoes, and again, you lose its content.


RE: F-String not working when in IDLE editor - nilamo - Jan-28-2019

This isn't anything special about how f-strings work, it's just how the interactive interpreter works. The interactive prompt will echo back the result of whatever expression you run, which is something that doesn't happen when running a script any other way (such as from the command line, or from IDLE).


RE: F-String not working when in IDLE editor - nadimsarrouh - Jan-29-2019

OK understood.

Thank you!