Nathan Schneider Nathan Schneider - 6 months ago 10
Python Question

How do I add text after a "print ("... " + ...)?

I am trying to make a Python script that is a choose your own adventure game. I'm asking what the day is.

day = input ("What is todays date?")
print ("Happy " + day)


I want to add an exclamation mark after
+ day
but it gives me an invalid syntax error.

Answer

For python 2.7

The first thing to note is that if you use input you ask for an integer (1,2,45,6,0,-5). And the thing you want to print is a string (a string are things you need to use quotes for).

To convert the input to a string you need to use str(). If you want to put the strings together you can simply do that with +

So it would become:

day = input ("What is todays date?")
print ("Happy " + str(day) + "!")

If using python 3.x you can drop the str().

Comments