Python Input and Output

In python, by using input() function you can read or get the user input. The input() method will read the keystrokes and return it as a string object.

 

Generally, the python will pause the code execution when it reaches the input() method and continues the code execution once it receives input from the user.

 

Following is the example of getting the user input using input() function in python.

 

name = input("Enter your name:")

print("Name: " + name)

When you execute the above python program, the code execution will stop at the input() function. Once the user enters the input value and clicks on Enter, the input() method will read the user input and assign it to a name variable.

 

Python user input and out example result

Python Display Output

In python, the built-in print() function is useful to display output to the user. By using the print() function, you can display the output in different formats.

 

Following is the example of displaying the output to the user using the print() function in python.

 

print("Welcome to Tutlane")

print("Learn", "Python")

name = "Suresh"

age = 30

print(name, age)

print("Name:", name, "Age:", age)

print(name, age, sep = ",")

If you observe the above example, we are trying to display the output in different ways using print() function.

 

When you execute the above python program, you will get the result like as shown below.

 

Welcome to Tutlane

Learn Python

Suresh 30

Name: Suresh Age: 33

Suresh,30

In python, you can also format the user output by using format() method based on your requirements.

 

str1 = "Welcome to {}".format("Tutlane")

print(str1)

name = "Suresh"

age = 33

str2 = "Name: {}, and Age: {} years"

print(str2.format(name, age))

When you execute the above python program, you will get the result like as shown below.

 

Welcome to Tutlane

Name: Suresh, and Age: 33 years

To know more about formatting in python, refer python string format() method.

 

This is how you can get the user input and display it by formatting it based on your requirements.