Python: Print Variables and User Input

Print

The basic Print statement in Python is Print “” – note Print ” also works. You can choose either single or double quotes, but you cannot mix them in the same statement.

Print Variables

Printing variables in Python is a bit more complicated.

If the variable you want to print is a number, use the special string %d inside the quotes and % variable outside the quotes.

If you have more than one variable, you need to have a %d for each variable. Then place your variables inside () at the end of the print line.

pyprint.jpgT

To print a string, you use %s. Notice below you can mix strings and numbers in the same line.

pyprint1.jpg

You can use %r if you are not sure what value the variable will have. It will print both.

User Input

raw_input() allows you ask the user for input.

You can assign the user input to a variable.

You can also pre-define what kind of input you want. Note the error in the second instance. x = int(raw_input()) means I am looking for an integer as input.

pyprint2

If I enter an integer, I do not get the error.

pyprint3.jpg


If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT. 

Follow this link for more Python content: Python

One thought on “Python: Print Variables and User Input

Please Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s