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

Screenshot 2022-07-03 211010

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.

Screenshot 2022-07-04 203014

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.

 

Screenshot 2022-07-04 203207

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

Screenshot 2022-07-03 213335

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.

Screenshot 2022-07-03 213628

If I enter a string I get an error

Screenshot 2022-07-03 213915


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

Screenshot 2022-07-03 214240

Try your code in our online Python console: 

Previous Page: Printing and Variables

Next Page: Print with .format{}

Back to Python Course: Course

2 thoughts on “Python: Print Variables and User Input

  1. Pingback: Python: Printing with .format() – Analytics4All

Leave a Reply