How to get Input from User in Python

how-to-get-input-from-user-in-python

Input is an essential part of any programming language, including Python. It allows a program to receive data from users.

In Python, we can get input from the user using the input() function.

In this article, we will discuss how to get input from the user in Python with examples. So, let’s start without wasting time.

The input() function is a built-in function in Python, which takes input from the user and returns it as a string.

input([Prompt])

Here, the prompt is an optional parameter that is used to display a message to the user.

Let’s take a look at a few examples to understand how to get input from the user in Python.

Getting Input from User:

in this code, we are going to know how to get input from the user by using the input() function.

Code:

name = input("Enter your name: ")
print("Hello, " + name + "!")

Output:

Hello Ali !

In this example, we are asking the user to enter their name using the input() function.

The message “Enter your name: ” will be displayed to the user. Once the user enters their name, it will be stored in the variable “name”.

Then, we are displaying the message “Hello, <name>!” to greet the user.

In this code, we are using the (+) operator to combine two strings.

If you want to get more information about Python Fundamentals you can visit here.

You can also write this code as follow;

Code:

name = input("Enter your name: ")
print("Hello, ", name ,"!")

Output:

Hello Ali !

you got the same output.

Converting Input to Integer:

As we know when we get input from the user the input() function always gives us a string. So, in this code, we going to know how to convert the input to an integer.

Code:

num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
sum = num1 + num2
print("The sum is", sum)

Output:

The sum is 7

In this example, we are getting two numbers as input from the user using the input() function.

Here input() function always returns a string, we are converting the input to an integer using the int() function.

Then, we are adding the two numbers and store the result in the variable “sum”. Finally, we are displaying the sum to the user.

Getting Multiple Inputs from User:

Here we are going to get two different types of inputs from the user and split them by using the split() function.

Code:

name, age = input("Enter your name and age: ").split()
print("Your name is", name, "and your age is", age)

Output:

Enter your name and age: Ali 20
Your name is Ali and your age is 20

In this example, we are getting two inputs from the user – their name and age.

We are using the split() function to separate the two inputs. The split() function splits the input string based on whitespace and returns a list of strings.

Since we have two variables – name and age, we can store the two values in separate variables using multiple assignments. Then, we are displaying the name and age of the user.

So, getting input from the user is a crucial part of Python programming. We can get input from the user using the input() function and manipulate it as required. We hope that these examples have helped you understand how to get input from the user in Python.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x