In our previous chapters, we came across several examples using the print() function to display the output. Similarly, we also have statements that are used for obtaining input from the user’s keyboard. Both Python input and output statements are critical in any project as it is the most basic functionality that a user performs on any application.
As mentioned above in the diagram, the input that a user enters undergoes some changes as per the logic of the program, and it gets printed in the output as our desired result. We can also have a logic to print the output as same as the input which the user provides. We will have a look at both the input and output statements of Python now.
Python Input statements
Python input statements are used for receiving input statements from the keyboard using the input() function. Let us look at an example now.
a=input() #the input from user's keyboard will be stored in a
print(a)
Output:
123
123
In the output mentioned above, when I entered 123, the same 123 was displayed as the output as per the logic I specified in the above program. Let us make this much better in the next example below.
name=input('What is your name? ')
print(name)
Output:
What is your name? iSapna
iSapna
Usually, the input entered by the user is stored as a string even if you enter the numbers. If you want to accept input only of a particular datatype, you can use the syntax as mentioned below. The code below allows only to accept the integer values from the keyboard. If any other values are entered, an error will be displayed in the output.
number=int(input('Enter a number: '))
print(number)
Output:
Enter a number: 123
123
Python Output Statements
Python output statements are used for displaying the output from a program using the print() function. If you use only print() without passing any value, it will throw the cursor to the succeeding line. Let us look at an example now.
print('i-Sapna')
Output
i-Sapna
In one of the previous chapters, “Python Comments, Literals, Constants, Docstrings“, we had a look at the Python Escape characters. Let us use some of them in the example below.
print("Mukesh \tand \nJayasuriya") #\t -- horizontal tab space and \n -- next line
Output
Mukesh and
Jayasuriya
You can use the ( * ) repetition operator to repeat the strings in the output, as shown in the example below.
print(7*"i-sapna")
Output
i-sapnai-sapnai-sapnai-sapnai-sapnai-sapnai-sapna
We know that the ‘+’ operator is used to perform additional operations in Python. However, if you use the same ‘+’ operator with the strings, it will join the separate string statements into one string. Let us look at an example below.
print("The name of "+"our planet "+"is called Earth")
Output
The name of our planet is called Earth
Using the print() function, you can also display the values of variables in Python. Let us look at an example below.
p,q=3,7
print(q)
print(p,q)
Output
7
3 7
In the example mentioned above, you can see that it displayed the value q on the first line and both the p and q values on the second line as intended. If you want to add a separator between variables when you include multiple variables in your output statement, you can use the ‘sep’ attribute as shown in the example below.
p,q=3,7
print(p,q, sep="--")
print(p,q, sep=":")
print(p,q, sep=",")
Output
3--7
3:7
3,7
You can also see that each print() function displays the output in a new line. However, if you want to make the output of several print() functions to display on the same line, you have to use the ‘end’ attribute as shown in the example below.
print("This", end='')
print("is cool", end='')
print("and fresh", end='')
Output
Thisis cooland fresh
You can see in the output above that though all print() functions were displayed on the same line, there were no spaces between them, and you can use the ‘\t’ tab space escape character in such cases to make the output look much better as shown in the example below.
print("This\t", end='')
print("is cool\t", end='')
print("and fresh\t", end='')
Output
This is cool and fresh
You can also pass objects like dictionaries, tuples or lists to the print() function to display elements from those objects as shown in the example below.
list1=[20,'Air','Water']
print(list1)
dictionary1={20:'Sandwich',30:'French fries',40:'Burger'}
print(dictionary1)
Output
[20, 'Air', 'Water']
{20: 'Sandwich', 30: 'French fries', 40: 'Burger'}
We know that the print() function is often used to display variables along with strings as shown in the example below
x=3
print(x,"is a prime number")
burger_count=40
print("There are ",burger_count,"burgers in our shop")
Output
3 is a prime number
There are 40 burgers in our shop
We can format the output displayed by the print() function as we wish. You can use the special operator % (percent) for this mission. Using this % operator, you can join a string with value or variable in the below format.
print(“formatted string” % (variables list))
In the place of formatted string, you can use %d or %i to show decimal integer numbers and %f to display float values. You can use %s to show strings. Let us look at the examples below
a=7
print('The value is %i'% a)
Output
The value is 7
As mentioned above, if you want to display a single variable, you don’t need to enclose the variable inside parentheses. But if you want to display more than one variable, you need to follow the code as mentioned below.
a,b=2,4
print("a=%i, b=%d" %(a,b) )
Output
a=2, b=4
You can use %s to display string in the formatted string. If you use %10s, it will add 10 spaces and then display string, and if you add %-10s it will add 10 spaces after the string as shown in the example below.
company='i-Sapna'
print('Welcome to %s'%company)
print("Welcome to %10s"%company) #adds 10 spaces before the string
print("Welcome to %-10s"%company,"end check") #adds 10 spaces after the string
Output
Welcome to i-Sapna
Welcome to i-Sapna
Welcome to i-Sapna end check
You can also use %c to display each character from the string as shown in the example below.
company='i-Sapna'
print("First and second characters are %c and %c"%(company[0],company[1]))
Output
First and second characters are i and -
You can use %f to display the floating-point values. If you use %20.3f, it will add 20 spaces before the value and display only 3 numbers after the decimal point. If you use %.4f, it will display only 4 numbers after the decimal point. Refer to the examples below.
number=987.654321
print("The declared number: %f"%number)
print("Number is: %20.3f"%number) #adds 20 spaces before value and only 3 values after decimal point
print("The value is: %.4f"%number) #adds 4 values after decimal point
You can also use a pair of curly braces { } to denote a replacement field and mention indexes or names in these replacement fields. These indexes or names follow the order of values. After mentioning the formatted string, we should write the member operator and format() method as mentioned in the format below
print(‘format string with replacement fields’.format(values))
If you want to display only a single value using index in the replacement field, refer to the example below.
a1,a2,a3=10,20,30
print("number in a1={0}".format(a1))
Output
number in a1=10
If you want to display all three numbers, refer to the example below.
a1,a2,a3=10,20,30
print("number in a1={0}, number in a2={1}, number in a3={2}".format(a1,a2,a3))
Output
number in a1=10, number in a2=20, number in a3=30
In the above example, {0}, {1}, and {2} represent the a1, a2, and a3 values respectively. That’s why if you change the order, it will display the values in a different order as shown in the example below.
a1,a2,a3=10,20,30
print("number in a1={2}, number in a2={0}, number in a3={1}".format(a1,a2,a3))
Output
number in a1=30, number in a2=10, number in a3=20
You can also refer to the examples below which display the same output in several ways
car,price="Toyota",400000
print('The car, {0} costs {1} rupees'.format(car,price))
print('The car, {a} costs {c} rupees'.format(a=car,c=price))
print('The car, {:s} costs {:.3f} rupees'.format(car,price))
print('The car, %s costs %.2f rupees'%(car,price))
Output
The car, Toyota costs 400000 rupees
The car, Toyota costs 400000 rupees
The car, Toyota costs 400000.000 rupees
The car, Toyota costs 400000.00 rupees
We hope you have understood the Python Input and Output statements clearly. If you are a little confused about this topic, try practising each example mentioned in this post. Also, please do not hesitate to share your views or ask your queries on Python Input and Output Statements in the comments section below.