-1

I am fairly new to Python, I have just finished setting it up on Visual Studio Code, and chose the right course..

yet with the following exercise i got a syntax invalid error,

my_name = "First Name"
print(f"My name is{my_name}.")

what am I messing?

The error:

File "c:\Users\Lenovo\Desktop\Pie\Hello.py", line 2
  print(f"Let's talk about {my_name}.")
                                    ^
SyntaxError: invalid syntax
Misha
  • 109
  • 8
  • 2
    What exact version of Python are you using? Your version may not support string interpolation (`f""`). – Carcigenicate Jan 24 '19 at 13:20
  • @Carcigenicate python 3.6.0 64-bit – Misha Jan 24 '19 at 13:21
  • 2
    If you find that fstrings are not working, you're probably using a version of Python before 3.6. You should include the text of the syntax error in your question. – khelwood Jan 24 '19 at 13:22
  • What is the exaxt error? It should include a `^` that points to where the error is. – Barmar Jan 24 '19 at 13:22
  • Strange, make sure you're not using 2.7.X python version. This looks to me like a classic 2.7.x python version problem – Gabriel Panza Jan 24 '19 at 13:23
  • 4
    When asking questions about errors, always include the copy-pasted (as text) full and complete error message. Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 24 '19 at 13:25
  • try importing sys and print out sys.version. You can see what python version you are running. – Stef van der Zon Jan 24 '19 at 13:26
  • @khelwood I have included the error, I am sure I have downloaded python from the website. – Misha Jan 24 '19 at 13:30
  • @Barmar included it in Q – Misha Jan 24 '19 at 13:30
  • @Misha Just because you've downloaded Python 3.6 does not mean that you are _using_ Python 3.6. – khelwood Jan 24 '19 at 13:30
  • @GabrielPanza I had 2.7 installed before and used it on notepad++ , now I shifted to VS and Python 3, I read earlier it's okay to have both versions installed. – Misha Jan 24 '19 at 13:31
  • @Misha you have to make sure by looking in console which version you are using – Gabriel Panza Jan 24 '19 at 13:33
  • See https://stackoverflow.com/questions/8917885/which-version-of-python-do-i-have-installed for how to find out the actual version you're running. – Barmar Jan 24 '19 at 13:35
  • Just a random question.. what do you really want to do? Do you really need to use the "f" method instead of ```print("%s" % name)```? – akhavro Jan 24 '19 at 13:35
  • @akhavro I am using it because it is recommended since I am going to be operating python 3.. and it is used throughout the book I am learning from. – Misha Jan 24 '19 at 13:40
  • @Misha I just asked because since you're a beginner, and this problem apparently is really due to the python version you're using, maybe it would be wiser to use the "old-style" prints instead of focusing too much effort on this. But ofc, it's your call. There is no advantage in using one print() way or another, except the readability. Will leave you now ;) Good luck. – akhavro Jan 24 '19 at 13:47

2 Answers2

0

I would write the code like this:

my_name = "First Name"

print("My name is " + my_name + ".")

It looks very easy and it is, but this would work.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
-4

I would recommend using the following syntax:

print("My name is %s" %my_name)
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117