Input/Output using Python input() and print() functions

Filed Under: Python

In this tutorial, we will cover the basics of Python Input/Output and also how to import a module in a Python program. Previously we learned about Python Datatypes.

Basic Input/Output Using Python

I/O means Input and Output. We will learn about the basic input and output functions below. Let’s start out!

How to Read User Input in Python with the input() function?

To accept user input in Python, you need to use the input() function.

With the input() function, you can read from the keyboard input until a line feed is given ( i.e. until you hit the Enter key).

#takes input from keyboard and stores in a string
str = input("Enter your input: ");

#then we can print the string
print(str)

If you run this program you will see a prompt like the below picture, waiting for you to give input. The user can enter multiple characters until they hit the enter key.

python Input Output, Python Import, keyboard input prompt

How to Print Output In Python?

We are already familiar with the print() function. We use the print() function to print output to the screen.

print("Print something to the screen")

Moreover, we can pass two or more different strings in the print function separated by either commas (,) or plus (+) signs. Like this;

print("This is first String " + "Followed by this string");
#Or can be written like this also
print("This is another String " , "Followed by another string");

Process the user inputs in Python by importing modules

Now moving on from the Python inputs and outputs, how do we process the data that is entered as input by the user?

We need certain functionality to the code. And for the same, we have two options:

  • Build the functionality from scratch
  • Use existing libraries to extend our program

Python is so widely accepted, using libraries is the easiest and the most preferred route. Python offers hundreds of thousands of libraries to work with.

So let’s work with the user input in Python with some added functionality.

Suppose the user inputs a number and we want our code to output the square root of the number. Now there’s a sqrt() function available Python.

But if we use the function directly, there will be an error as shown in the output below:

#get a variable having value 16
number=16

#square root this number.
number=sqrt(number)

print(number)

python import, python import module error

This is because, sqrt() function is under module name “math”.

If we want to use this function, we need to make this module accessible by importing the module. So, let’s see how importing the module will provide added functionality for our code here.

#first import math module
import math
#get a variable having value 16
number=16

#square root this number.
number=math.sqrt(number)

print(number)

If you run this code, you’ll get the square root of the number 16 which is 4.

Conclusion

Awesome! I hope you learned all the basics you need to accept user input in Python and print the required outputs for the same.

We have also learned how we can import certain modules in Python to extend the functionality of our code and work with the inputs.

Comments

  1. Adebola Adesina says:

    The input received from the input function is always a string, so you need to type cast it to a int or float. Else you will get you a type error.
    number = sqrt (int(number))

  2. Lee says:

    Very helpful so far, thanks man!

  3. Ashu says:

    while reading first few characters, i get the following error.
    Could you please help out?

    “Traceback (most recent call last):
    File “”, line 1, in
    str=f.read(5)
    File “C:\Program Files\IBM\SPSS\Statistics\Subscription\Python3\lib\encodings\cp1252.py”, line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x81 in position 61: character maps to “

    1. Vani says:

      May be you have the special characters in your file. Use encoding=”utf8″ in parameter for open like f = open(“input_file.txt” ,’r’, encoding=”utf8″), your issue should be resolved.

  4. Taufik says:

    Very good series of articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content