2

When I type the following into Notepad++ as Python code:

days = "Mon"

print "Here are the days: %s ". % days   

I get this output in Windows Powershell:

File "ex9exp.py", line 4
print "Here are the days: %s ". % days  

I am at a loss as to why this is happening.

I intend for the output of

print "Here are the days: %s ". % days 

to be

Here are the days: Mon  

Would appreciate some help.

Raz
  • 93
  • 1
  • 6
  • 1
    Why the dot after the string? Also how are you running the code? – Maroun Jan 04 '15 at 15:40
  • 1
    If you are getting a syntax error remove the . after the string "Here are the days: %s " – avenet Jan 04 '15 at 15:41
  • Ah ok - just removed the dot and it worked! Thanks. Am quite new to this. If you could reply in a comment explaining why the dot led to it not working that would be very grand! Am a beginner and am learning this as I go along. – Raz Jan 04 '15 at 15:42
  • OK @Raz, I explained the problem in the answer. Thank you... – avenet Jan 04 '15 at 15:56

3 Answers3

1

The problem is that the syntax of the print function you are using is wrong.

>>> days = "Mon"
>>> 
>>> print "Here are the days: %s ". % days   
  File "<stdin>", line 1
    print "Here are the days: %s ". % days   
                                    ^
SyntaxError: invalid syntax

Remove the .. and try

>>> print "Here are the days: %s " % days   
Here are the days: Mon 
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1

The . after the string is an operator used in some languages like PHP to append strings, but it does not work in Python, so a code like this:

days = "Mon"

print "Here are the days: %s ". % days   

Produces the following output:

   File "h.py", line 3
     print "Here are the days: %s ". % days
                                     ^ SyntaxError: invalid syntax

Letting you know the compiler/interpreter was not expecting a ".".

The problem can be fixed removing the ., like this:

days = "Mon"

print "Here are the days: %s " % days 

It will work that way.

avenet
  • 2,894
  • 1
  • 19
  • 26
0

You can also use + to append strings

print "Here are the days: " + days

will work

Gadi
  • 1,152
  • 9
  • 6