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.

4
  • 1
    Why the dot after the string? Also how are you running the code? Commented Jan 4, 2015 at 15:40
  • 1
    If you are getting a syntax error remove the . after the string "Here are the days: %s " Commented Jan 4, 2015 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. Commented Jan 4, 2015 at 15:42
  • OK @Raz, I explained the problem in the answer. Thank you... Commented Jan 4, 2015 at 15:56

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

0

You can also use + to append strings

print "Here are the days: " + days

will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.