Tuesday, April 7, 2026

Python Tutorial for Beginners: User Input

From the previous chapters, we learned how to use string, loop, functions and methods to write a code in Python language. Now we can go one step ahead and learn how to take a input from the user.

Suppose, we are going to design an application and also want to intract with user or use in real world. Then it is must to learn how to accomplish this. This concept is very important to go further in this python tutorial, so ensure you understand every line and practice as much as possible.

What is input function?

The Input function is used to get input or data from the user via the keyboard. It stores the value provided by the user in a variable.

Syntax: input("Message")

Example:
name = input("Enter Your Name:  ")
print(name)             #Output: Anything you type is convert into output including number.
Note: If you type any number, it also appears in the output. From this, you can understand that the "input()" function always returns data as a string, even if the user enters a number.
Example: With Number
age = input("What is your age: ")
print(age)
print(type(age))      #Show data type
Now, as you can see in the image, the output of the data type is String (str).

Convert Input to Interger

If you want to use the input as an integer, you can convert it using the int() function.

age = int(input("What is your age: "))
print(age)
print(type(age))      #Show data type      

Your value is convert to integer.

How to take two input from user?

print("Add Two Numbers")
Num1 = int(input("Enter First Number: "))
Num2 = int(input(Enter Second Number: ")) print(Num1 + Num2)                 #print addition of give number      

Taking String Input

When we want to generate output with user string input then we can create default value with print function.

name = input(Enter Your Name: ")
city = input("Enter Your City: ")
print("Hello", name)
print("You live in", city)      

Convert Input to Float

If we want number in decimal then we can use Float().

price = float(input("What is the price: "))
print(price)      

Create Simple Calculator with Input() 

print("This is simple calculator")
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
print()
print("Addition", num1+num2)
print("Substraction", num1-num2)
print("Multiply", num1*num2)
print("Divide", num1/num2)

How to take multiple input in one line?

You can also take a input in one line.

a,b = int(input(Type Two numbers: ")).split()
print(a)
print(b)      

Tasks

  • Write a program to take user's name and age.
  • Write a program to add two numbers entered by user.
  • Write a program that asks for city and country.
  • Conclusion

    This is all about the input() method. Now practice this method as much as possible to stay ahead in Python. The next chapter will be "Exception Handling".

    No comments:

    Post a Comment

    Featured Post

    What Is Artificial Intelligence (AI) ? – A Step-by-Step Guide

    AI (Artificial Intelligence) is a rapidly growing technology in today's world, and you can see it whenever you browse or search for any ...