Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
format strings syntax error
#1
I'm a noob doing an introductory lesson on format strings. The example given in the lesson keeps giving an error, even when I directly copy/paste. Here's the code:

my_height = 80
print(f"He's {my_height} inches tall.")
The error I get says this:
Error:
File "/home/main.py", line 9 print(f"He's {my_height} inches tall.") ^ SyntaxError: invalid syntax
I looked for some examples online and was able to make it work like this:

my_height = 80
print("He's {} inches tall.".format(my_height))
So what's wrong with the syntax in the lesson I was using? Is the example in the lesson totally wrong?
Reply
#2
You must be using a python version before 3.6. f-strings as in your first snippet were introduced in python 3.6
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yup, that was it! I've been practicing using an online editor and when I checked I saw it was an older version. I found a 3.6 one and now it works.

Lesson learned, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,131 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 366 Jan-19-2024, 01:20 PM
Last Post: rob101
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 692 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Trying to understand strings and lists of strings Konstantin23 2 752 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Syntax error while executing the Python code in Linux DivAsh 8 1,526 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,196 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,282 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,234 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 878 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,076 Nov-27-2022, 04:10 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020