Python I/O – Take User Input and produce Output

In this Python article, we will discuss how to take input from the user or print the output on the screen.

1. How to take input from the user?

For reading the value from the user we use input() in python3.

input() function tells the interpreter to wait until the user presses some key or give input.

Note: In Python2 we prefer to use raw_input() instead of input() but the raw_input() was removed in python3.

Syntax : input([message(optional)])
line = input("Your Favourite dialogue ? \n")
print(line)
Output
Your Favourite dialogue ? 
It's a beautiful day to save lives
It's a beautiful day to save lives

Note: input() takes the input as a string so for any other data type we need explicit type conversion.

1.1. Taking integer as the input

input() by default take everything as a string, so to use our input as an integer, we will take the input() and then perform type conversion.

Let’s understand it with an example.

a = int(input("Enter a number - "))
print(type(a))
Output
Enter a number - 1064
<class 'int'>

1.2. Taking list as the input

It’s simple!!

As we did for integer same way we can convert the input using type conversion into the list, yes it would work but it will give the output something like this

Input: 1 2 3 4 5
After conversion it will be : ["1"," ","2"," ","3"," ","4"," ","5" ]

Because it will consider space as well and store every element in the list as a string, so to resolve this we have functions like map() and split()

  • split(” “): function will separate all the values according to space.
  • map(int, input()): function will map all the values as an integer, and then we will do the type conversion and get the list of integers.
Input: 1 2 3 4 5
After conversion it will be : [1, 2, 3, 4, 5]
a = list(map(int, input().split(" ")))
print(a[2])
Output
1 2 3 4 5
3

Remember: When we press enter without giving any input then input() or raw_input() function returns an EOF error. EOF stands for End of line.


2. How to print output to the screen?

In many examples, you might have noticed that we have used the print() function. It is used to print the message or data in the prompt.

The syntax of the print() function is:

Input: 1 2 3 4 5 
After conversion it will be : ["1"," ","2"," ","3"," ","4"," ","5" ] 

For concatenation, we use either use sep parameter of the print() function or we can also do string concatenation by using “+” operator.

We can pass a comma(,) separated list of parameters and they all will be printed separated by space because the default value of sep parameter is a space(” “), or we can use any other separator

2.1. Concatenating a string with an integer

Sometimes we do the calculation in print itself and want to print a message as well. So for that, we convert our integer result into the string using type conversion.

Let’s see the example to understand it better:

print("sum of 2 and 3 is " + str(2+3))

# Using default value of sep parameter
print("sum of 2 and 3 is", 2+3)

# Using ' -- ' as the separator
print("sum of 2 and 3 is", 2+3, sep = " -- ")

# Using ' + ' as the separator and ' = ' as the line end
print(1, 2, 3, 4, 5, sep = " + ", end = " = ")
print(15)
Output
sum of 2 and 3 is 5
sum of 2 and 3 is -- 5
1 + 2 + 3 + 4 + 5 = 15

2.2. Printing without a newline

When we write two different print statements then the next one automatically goes to the next line. It happens because by default print statement adds a newline character(\n) at the end. So to print them on the same line we use end parameter of function print().

Let’s understand using an example, here we are ending the line using space and ‘@’ but we can use anything.

print("It's a beautiful Day", end = " ")
print("to save lives")

print("It's a beautiful Day", end = " @ ")
print("to save lives")
Output
It's a beautiful day to save lives
It's a beautiful Day @ to save lives

3. Difference between raw_input() and input() in Python2

raw_input()input()
Take the input exactly as the user typed and return it as a stringTakes the raw_input() and then perform eval() in it
raw_input does not expect syntactically correct python statementExpects syntactically correct python statement
Reads whatever is given as inputEvaluates the provided input
renamed as input() in Python3In python3 accessed using eval(input())
raw_input() vs input()

4. Conclusion

In this article we have discussed:

  • How to take input from the user.
  • How to print output in the prompt.
  • Difference between raw_input() and input() in python2.

Complete code samples are present on Github project.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.
Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x