Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f-strings giving SyntaxError?

I am getting an error message with my Atom reader here, where it is suggesting the first print.(f"message") is delivering an error:

File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75     print(f"Let's talk about {my_name}.")                                        ^ SyntaxError: invalid syntax [Finished in 0.077s] 

Code:

my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches my_weight = 180 #lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown'  print(f"Let's talk about {my_name}.") print(f"He's {my_height} inches tall.") print(f"He's {my_weight} pounds heavy.") print("Actually that's not too heavy.") print(f"He's got {my_eyes} eyes and {my_hair} hair.") print(f"His teeth are usually {my_teeth} depending on the coffee.") 
like image 452
Alexander Coffman Avatar asked May 17 '18 23:05

Alexander Coffman


People also ask

How do I fix Python Syntaxerror invalid syntax?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why %F is used in Python?

Python string formatting It uses the % operator and classic string format specifies such as %s and %d . Since Python 3.0, the format function was introduced to provide advance formatting options. Python f-strings are available since Python 3.6. The string has the f prefix and uses {} to evaluate variables.

What is F-string in Python?

“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.

Does Python 3.6 support F-strings?

Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498.


1 Answers

I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here

like image 145
InAFlash Avatar answered Sep 17 '22 13:09

InAFlash