Site icon Analytics4All

Python: Print Variables and User Input

Advertisements

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 by themselves is pretty straight forward:

Printing variables in line with some text is a little 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.

 

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

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

Try your code in our online Python console: 

User Input

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.

If I enter a string I get an error


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

Try your code in our online Python console: 

Previous Page: Printing and Variables

Next Page: Print with .format{}

Back to Python Course: Course

Exit mobile version