Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 F-String not working when in IDLE editor
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
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/ ================
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?
print(f"Hello, {name}. You are {age}.")
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?
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.
nilamo and metulburr like this post
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).
buran likes this post
OK understood.

Thank you!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  if and condition not working for string Staph 4 545 Jul-01-2019, 09:02 AM
Last Post: Staph
  python script file not opening in IDLE editor srm 2 841 Jun-23-2019, 08:45 AM
Last Post: Larz60+
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 1,403 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 906 Sep-14-2018, 07:54 PM
Last Post: Larz60+
  Python IDLE 3.6.2 on WIn7 vs Pyhton 3 IDLE raspberry djdan_23 5 2,998 Sep-07-2017, 12:51 PM
Last Post: Larz60+
  Return Statement in Python IDLE editor NMW 10 4,462 Jul-11-2017, 09:47 PM
Last Post: NMW

Forum Jump:

Users browsing this thread: 1 Guest(s)