Variables in Strings

I keep getting “Syntax Error: Invalid Syntax” with the following code that I’m following in my textbook:

first_name = "ada"

last_name = "lovelace"

full_name = f"{first_name} {last_name}"

print(full_name)

Any ideas what I’m missing here? Thanks!

I keep getting “Syntax Error: Invalid Syntax” with the following code
that I’m following in my textbook:

first_name = "ada"

last_name = "lovelace"

full_name = f"{first_name} {last_name}"

print(full_name)

Any ideas what I’m missing here? Thanks!

Can you show us the entire error message will all it’s accompanying
lines? Also, what version of Python are you running?

To use f-strings (the f"..." form) you need Python 3.6 onwards.

To give hope, here’s your code running in Python 3.10 here in IDLE (the
interactive Python prompt):

 Python 3.10.6 (main, Jan  1 2024, 19:58:15) [Clang 15.0.0 
 (clang-1500.1.0.2.5)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> first_name = "ada"
 >>> last_name = "lovelace"
 >>> full_name = f"{first_name} {last_name}"
 >>> print(full_name)
 ada lovelace
1 Like

Thanks so much for the quick reply! I’m running version Python 3.12.3 and the accompanying error message lines are:

SyntaxError: invalid syntax
/usr/local/bin/python3 /Users/kalvin/Desktop/python_work/full_name.py
File “”, line 1
/usr/local/bin/python3 /Users/kalvin/Desktop/python_work/full_name.py
^

Is the line /usr/local/bin/python3 /Users/kalvin/Desktop/python_work/full_name.py literally in your script?

2 Likes

Do not put this inside the source code file (remove it if it’s already there). Instead, type it in a terminal/shell window, after saving the file. (Exactly what operating system are you using?)

1 Like

… because it shouldn’t be - it’s an example instruction of how to
run your script.

1 Like

Hey everyone, thanks so much for the prompt replies. I tried opening a new file (I’m using a Mac with the latest version of python) and typed in the same code under a different save file and it didn’t work again. Exact same error code.

THEN I tried the code in sublime text editor and it worked. I still have no idea why it’s not working in vis studio though =/

I put visual studio python command syntax error into a search engine and the first result was:

which appears to be the exact issue you describe.

If you know what’s in your source code file and you’re getting an error message that tells you about something that isn’t in your source code file, you should suspect the IDE.

2 Likes

Ah, thank you! :pray::blush: