DEV Community

Cover image for Python get input from user
Baransel
Baransel

Posted on

Python get input from user

Sign up to my newsletter!.

input()

Let's show the use of the input() function simply by taking the name from the user.

name = input("Enter your name: ")
print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your name: Baransel
Baransel
Enter fullscreen mode Exit fullscreen mode

As seen above, we received a name information from the user. We printed the information we received with the print() function, which we processed in our previous lesson.

For example, let's take two numbers from the user and add them

number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")

sum = number1 + number2
print("Total: ", sum)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter the first number: 52
Enter the second number: 45
Total: 5245
Enter fullscreen mode Exit fullscreen mode

As you can see, we got the result of 1225, it actually gave the numbers 12 and 25 written side by side, not the sum of these two numbers.

With the input() function, we can only get String (text) data types from the user.

In other words, it took numbers in String type, so how do we import data of Integer (integer) type?

number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")

sum = int(number1) + int(number2)
print("Total: ", sum)
Enter fullscreen mode Exit fullscreen mode

format() method:

Continue this post on my blog! Python get input from user.

Sign up to my newsletter!.

Top comments (0)