Practical Application for Python: Using Print and Input

Instructor: Jerry Cavin

Jerry Cavin is a Computer Scientist, Adjunct Professor, author and amateur astronomer. He has a MS in Computer Science and Astronomy.

This lesson will teach a practical application of Python's print and input methods. We will demonstrate the print() statement by outputting fixed data to the screen, and then demonstrate how input() can output data that isn't quite so predictable.

Lesson Overview & Knowledge Required

Welcome! In this lesson, we will look at a practical application of two fundamental concepts: output and input. In Python, we usually associate output with print() and input with input(). We're going to learn how to do two things:

  1. Output a fixed statement to the screen ('This is a simple Python program')
  2. Get random input from the user console (which we can then print to the screen)

These two techniques will help us go from an unchanging program (that always prints the same thing) to something more useful (a program capable of printing a variety of things).

To learn Python I highly recommend that you download the Python interpreter and enter each example to see how it works. The Python interpreter can be downloaded from the website ( www.python.org ). Python 3.7.0 is currently available on the site. All examples in this lesson have been generated using version 3.7.0

An example of how to use the interpreter follows.

  • Goto the File drop-down and create your file (in this case 'HelloWorld.py)
  • Enter the text print('Hello, world!) in the interpreter window
  • Save the file. You can now select Run to execute the file.

Python Hello World execution

Program Code

Objective 1: Using Print in Python

The first part of our lesson is about print(). How do we tell it what we want the output to look like?

The print() function in Python formats the data and sends it to the output in many formats. To explain, should Python treat our outgoing, formatted data as literal text (ASCII codes) or as actual numbers? What kind of numbers (integer, real, etc.)?

The following sections provide a few basic examples.

The print() function is used to output data to the standard output device (usually the screen). For example:

>> print('This is a simple Python program.')

This is a simple Python program.

Python Conversion Table

Let's look at examples of these print conversion types in action:

>>> print('%10.3e'% (3.14159))

3.142e+00

>>> print('%10.3E'% (3.14159))

3.142E+00

>>> print('%10o'% (314))

472

>>> print('%10.3o'% (31))

037

>>> print('%10.6o'% (31))

000037

>>> print('%5x'% (3141))

c45

>>> print('%5.5x'% (314))

0013a

>>> print('%5.4X'% (31))

001F

Formatting Output with 'Flags'

Another option to format strings are with flags:

Flag Conversions

Let's look at examples of print with flags:

>>> print('%#5X'% (3))

0X3

>>> print('%5X'% (31))

1F

>>> print('%#5.4X'% (31))

0X001F

>>> print('%#5o'% (31))

0o37

>>> print('%+d'% (31))

+31

>>> print('%+2d'% (31))

+31

>>> print('% 2d'% (31))

31

>>> print('%2d'% (31))

31

Formatting Output with the 'width' option

The width option is a positive integer specifying the minimum field width. If the converted value is shorter than width, spaces are added on left or right (depending on flags):

>>> print('(%30s)' % 'right justify')

( right justify)

>>> print('(%-30s)' % 'left justify')

(left justify )

Formatting Output with the 'precision' Option

The symbol for precision is a dot (.) followed by a positive integer. Note the use of the %f conversion specifier here:

>>> print('%.2f' % 3.14159)

3.14

Printing with Dynamic Formatting

Sometimes in programming, you may want to format a string but you do not know what size it will be. In this case, you can print it as a standard length using dynamic formatting using the * character:

>>> print ('%*s : %*s' % (20, 'John', 20, 'Smith'))

John : Smith

Objective 2: Using Input in Python

The ability to output stuff to the screen is cool, but it would get old very quickly if it never changed (we would always know what was going to happen). A Python programming can do more than just obey a set of instructions; it can ask the world questions and respond to the replies in a variety of ways. That not only makes this more interesting, it makes our programs more useful and surprising. How does Python ask questions? One way is using the input() method.

There are very few programs that operate without data somehow being entered into the system. Some of the ways data are entered are through: another computer, a port, a network, a mouse, or a database - but most often data are entered by a person on a keyboard. To do this Python provides the input() function.

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Free 5-day trial

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create an account
Support