In this part of the Python Tutorial, we will explore how standard input and output work in the Python programming language. By the end of this lesson, you’ll have a complete understanding of how to handle standard input and output in Python.
Hello, & Welcome!
Part 5 – Working with Standard Input and Output in Python
In this section of the Python Tutorial, I will explore the following topics –
- Python Output- print() function.
- Python Input- input() function.
- Output Formatting – format() function.
- How to comment in Python?
Python Output- print() function.
In Python, the built-in print() function is used to show output on the console or standard display screen. For example, you can show a message to the user using the print() function.
Syntax of print() function is- print(message)
Let’s have a look at the example-
>>>print("hello! My name is John")
OUTPUT-
hello! My name is John
You can also display any value using the print() function, along with the message you want to show. Let’s see in the example-
>>>x=56
>>>print("Your age is",x)
OUTPUT-
Your age is 56.
You can display multiple values in a single print statement by using concatenation. Let’s see in the example.
>>>name=input("Tell your name")
Tell your name John
>>> age=input("Tell your age:")
Tell your age:25
#Use ' ' to concatenate.
>>> print(name,' you are',age)
OUTPUT-
John you are 25
Python Input- input () function.
Up to this point, we’ve been assigning values directly within the program. But using input () function user can input values of their choice.
The input () function enables users to input data of their choice during program execution.
Let’s take a look in the example-
>>>age= input("Tell your Age:")
Tell your Age: 25
>>>print(age)
OUTPUT-
25
- However, the age entered by the user, such as “25”, is treated as a string rather than a numeric value. You can convert a string to a numeric value by using either int() or float() function.
Let’s take a look in the example
>>>int('25')
OUTPUT-
25
>>>float('25')
OUTPUT-
25.0
Program for calculating the Area of a Circle-
The formula for calculating the area of a circle is: π × r².
- Where r is a radius,
- π value is 3.142
>>>radius=input('Enter radius of circle(m):')
Enter radius of circle(m):5
>>> area=3.142*int(radius)**2
>>> print('The area of circle is',area)
OUTPUT-
The area of circle is 78.55
Output Formatting – format () function.
Normally, we place our code inside the print () function to display output, but by using the format () method, we can enhance the appearance and structure of our output.
Let’s take a look at an example to understand how the format () method works.
>>> Var1=5
>>> Var2=7
>>> print('value of Var1 is{0} and Var2 is {1}'.format(Var1,Var2))
OUTPUT-
value of Var1 is 5 and Var2 is 7
- Here, {0} and {1} refer to the first index position and the second index position, respectively.
- If you’re working with a floating-point number and want to limit the number of decimal places – for example, converting 3.14254678 to just 3.14 – you can easily do that.
Let’s see how you can do it-
>>> x=3.14254678
>>> y=10.2903348
>>>print('value of x is {0:.3} and y is {1:.3}'.format(x,y))
OUTPUT-
value of x is 3.14 and y is 10.3
- In {0:.3}, the .3 denotes the precision, restricting the decimal limit to three digits.
- It will display only 3.14 which correspond to the three-digit limit.
- If you prefer to treat 3.14 explicitly as a float with three digits after the decimal point then write it as: {0:.3f}
That’s all for our discussion on Standard Input and Output in Python. Hopefully, now you have a clear understanding of how it works.
Congratulations! You’ve successfully mastered the use of standard input and output in Python.
We’ll begin exploring If-Else statements in the upcoming tutorial.
Enjoy Learning Python!