Formatted string literals in Python 3.6+
I was reading into formatted string literals (a.k.a f-string
) which is a feature added from the Python 3.6 update. I find it easier to read then the previous ways to doing similar projects, but I am having trouble executing the code. Here an example (test.py) of what I made so far:
#!/usr/bin/python3
greeting = 'Hello'
name = 'World'
message = f'{greeting}, {name}.'
print(message)
As you can see, it is simple code I used to get more familiar with this f-string
feature. When I try to run this code I keep getting this error:
File "/home/user/Desktop/Python/My Programs/test.py", line 5
message = f'{greeting}, {name}.'
^
SyntaxError: invalid syntax
[Finished in 0.0s with exit code 1]
Now this error response is telling me that the final ( ' ) is what is causing this not to run correctly. I have tried to space it differently to see if it could be as simple as that, but still no luck. Here is a look into my setup that I use to write and execute Python code:
Text Editor:
I am currently using Sublime Text
with features added to help code Python3 code. These features include: BracketHighlighter
and Anaconda
packages. I have also changed some settings within Sublime Text, but I don't believe that is causing this issue because I mainly only changed graphical features.
Extras:
At first I thought Sublime Text might be trying to execute the Python3 code with a lower version of Python, so when I tried to execute it via Terminal in Ubuntu, it still gave me the same error. I executed the code with this:
python3 test.py
Hopefully this is enough information to where someone can spot out the issue. I really want to wrap my head around these f-strings
so any help is greatly appreciated.