Python Input and Output

In this tutorial, we will go through learning how to take input from users and print them using different formatting available at Python.

Before taking input, let’s see how we can print something in Python.

How to print variables or values in Python?

The print() function helps to print output data to the standard console device screen. To understand it, you can see the following example:-

The output of the above program will be

Hello World
John Doe
The general syntax for the print statement is:-
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

How to format output in Python?

To format the output in Python,  format() method is used which allows simple placeholder for formatting.

There are four different ways of handling formatting in Python.

  • Default arguments
  • Positional arguments
  • Keyword arguments
  • Mixed arguments

Let us explore with some examples:-

The output of the above program is:-

Hello John, your marks roll number is 79.
Hello John, your roll number is 79.
Hello John, your roll number is 79.
Hello John, your roll number is 79.

How to take input from users in Python?

Taking input in Python is much simpler than in any other programming language. The use of input() function will get a prompt in the screen and ask for input. The value is assigned to the variable that is used to take the input from the user.

Let us use the following program to make a better understanding of taking input from users and printing them in the screen.

The output of the above program is:-

Hello, please input your name: John Doe
Your name is John Doe

Please, note that the input that we took from the user is always a string. So, if you need some integer or float operation, make sure you use appropriate type conversion. Learn more about Python Data Type Conversion.

SHARE Python Input and Output

You may also like...

Leave a Reply

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

Share