Python f-string problem

Hello. First day learning Python.

I am learning from “Python Crash Course 2nd edition”, currently on page 21.

I am learning about Using Variables in Strings.

For some reason, I get an error message for this:

first_name = “ada”
last_name = “lovelace”
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}!")

I copied the book exactly but still get an error message. What am I doing wrong?

King Regards, Buttermilk

There’s nothing obviously wrong. Please share the error message to get help (that’s a good principle in general).

Also when sharing code it’s helpful to enclose it in a markdown code block, like so:
code goes here

That will look like this:

code goes here

Most importantly it preserves indentation, which might be part of the problem.

1 Like

Hey, thanks for taking the time to help me.

Here is the code:

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}!")

error message:
File “/Users/MYNAME/Desktop/python_work/”, line 3
full_name = f"{first_name} {last_name}"
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u “/Users/seanfairless/Desktop/python_work/”]
[dir: /Users/seanfairless/Desktop/python_work]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

I’m using a MacBook Pro and sublime text editor.

Kind Regards, Buttermilk

That’s really odd, the code works just fine if I run it in the interpreter (Python 3.8):

>>> first_name = "ada"
>>> last_name = "lovelace"
>>> full_name = f"{first_name} {last_name}"
>>> full_name
'ada lovelace'
>>> full_name.title()
'Ada Lovelace'

That makes me wonder if there’s some issue with a non-printing character (e.g. a line break). If your editor has a way to show non-printing characters you should enable that to see if anything’s off.

Or you could try typing (not copying) the code into the interpreter command line. If that works most likely something’s wrong with the file.


Are you using python 3? In a terminal type python --version.

If you run that segment in python 2.* you’ll get a syntax error for the last character, ", of the third line. (the same line where you said you got a syntax error)

In linux:

$ python --version
Python 2.7.18
$ python3 --version
Python 3.8.5

Hello guys. Thank you for the helpful replies.

I inputted the code into my terminal and it worked. So, the problem was with sublime text editor.

I created a new 'New Build System" - code:

"cmd": ["python3", "-u", "$file"],

I saved this onto my computers users file, then ran the program again. Worked perfectly!


1 Like