Bottom Page

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

Thank you!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  IDLE not responding (Mac) Selinal 1 72 Aug-05-2019, 10:27 PM
Last Post: Larz60+
  Using IDLE, bug when clicking code StarBasket 4 134 Jul-31-2019, 12:30 PM
Last Post: Malt
  Looking for a "custom" script editor NewBeginnings 12 344 Jul-21-2019, 03:57 PM
Last Post: snippsat
  Running python from Sublime Text editor balley810 2 149 Jul-14-2019, 03:40 PM
Last Post: ThomasL
  if and condition not working for string Staph 4 195 Jul-01-2019, 09:02 AM
Last Post: Staph
  python script file not opening in IDLE editor srm 2 213 Jun-23-2019, 08:45 AM
Last Post: Larz60+
  Can't write "\" backlash in IDLE Zherot 10 3,388 Jun-23-2019, 06:42 AM
Last Post: nurisyazoya
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 402 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  IDLE keyboard shortcuts tam 2 254 May-15-2019, 06:34 AM
Last Post: tam
  IDLE Deployment to classroom siwekp 1 155 May-14-2019, 07:21 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)