Python Tutorial: Standard Input, Output in Python

Standard Input in Python

This is the Python Tutorial: Standard Input, Output in Python. In this Python Tutorial, you will learn the Standard Input, Output in the python language. At the end of this tutorial, you will have full knowledge of Standard Input, Output.

Hello, & Welcome!

Part 5- Python Tutorial: Standard Input, Output in Python

In this article of Python Tutorial, you will learn following-

  1. Python Output- print() function.
  2. Python Input- input() function.
  3. Output Formatting – format() function.
  4. How to comment in Python?

Python Output- print() function.

In python, for printing something on the console or the standard output screen, we use a simple print() function.

For example, if you wanna print some messages on the console, you can print it by using a print() function.

Syntax of print() function is- print(message)

Let’s have a look in the example-

>>>print("hello! My name is John")

OUTPUT-
hello! My name is John

you can also print any value by the print() function, along with the message you wanna print. Let’s see in the example-

>>>x=56
>>>print("Your age is",x)

OUTPUT-
Your age is 56.
  

If you wanna print more than one value in one print statement, then you can do it by 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.

Till now, we are providing value to the program, but if you want that user will give input value, then there is a function – input().

Input() function allows users to provide the value of their choice.

Let’s take a look in the example-

>>>age= input("Tell your Age:")
Tell your Age: 25
>>>print(age)

OUTPUT-
25
  • But, here the value entered by the user as an age “25” is a string, it is not a number. For converting this into a number, int() or float() functions are there.

Let’s take a look in the example-

>>>int('25')
OUTPUT-
25

>>>float('25')
OUTPUT-
25.0

Program for calculating the Area of Circle-

The formula for the area of circle = πr2

  • 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.

In a normal way, we write our code in a print() statement, but we can use format() method to make our output more attractive and structured.

Let’s see in the example, how to use format() method.

>>> num1=5
>>> num2=7
>>> print('value of num1 is{0} and num2 is {1}'.format(num1,num2))

OUTPUT-
value of num1 is 5 and num2 is 7
  • Here {0} represents index value 0, and {1} represents index value 1.

If you have float number and you wanna restrict the decimal number in some limit, like 3.14254678 to only 3.14 so, you can do it.

Let’s see how you can do it-

>>> a=3.14254678
>>> b=10.2903348
>>>print('value of a is {0:.3} and b is {1:.3}'.format(a,b))

OUTPUT-
value of a is 3.14 and b is 10.3
  • Here, in {0:.3} the .3 decides the decimal limit to- .3
  • It prints only 3.14 which is .3 decimal place.
  • If you want that it will consider 3.14 as float, then you can write it as- {0:.3f}

That’s all for Standard Input, Output in Python, I hope now you have a better understanding of Standard Input, Output.

congratulations! You successfully learned Standard Input, Output in python.

Python Tutorial: Strings in Python

In the next tutorial, we will start learning If-else Statements.

Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

“Live as if you were to die tomorrow. Learn as if you were to live forever.” 

Mahatma Gandhi

author image

Written By Aqsa Zafar

Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.

1 thought on “Python Tutorial: Standard Input, Output in Python”

  1. Pingback: Python Tutorial: List in Python- Beginners Guide(2020)

Leave a Comment

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