Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Taking input from console in Python
Next article icon

Python input() function is used to take user input. By default, it returns the user input in form of a string.

input() Function 

Syntax: 

input(prompt)

prompt [optional]:any string value to display as input message

Ex: input("What is your name? ")

Returns: Return a string value as input by the user.

By default input() function helps in taking user input as string.
If any user wants to take input as int or float, we just need to typecast it.

Refer to all datatypes and examples from here.

Python input() Function Example

Python
# Taking input as stringcolor=input("What color is rose?: ")print(color)# Taking input as int# Typecasting to intn=int(input("How many roses?: "))print(n)# Taking input as float# Typecasting to floatprice=float(input("Price of each rose?: "))print(price)

Output:

What color is rose?: red
red
How many roses?: 10
10
Price of each rose?: 15.50
15.5

Example 1: TakingtheName and Age of the user as input and printing it

By default, input returns a string. So the name and age will be stored as strings.

Python
# Taking name of the user as input# and storing it name variablename=input("Please Enter Your Name: ")# taking age of the user as input and# storing in into variable ageage=input("Please Enter Your Age: ")print("Name & Age: ",name,age)

Output:

Please Enter Your Name: Rohit
Please Enter Your Age: 16
Name & Age: Rohit 16

Example 2: Taking two integers from users and adding them.

In this example, we will be looking at how to take integer input from users. To take integer input we will be usingint() along withPython input()

Python
# Taking number 1 from user as intnum1=int(input("Please Enter First Number: "))# Taking number 2 from user as intnum2=int(input("Please Enter Second Number: "))# adding num1 and num2 and storing them in# variable additionaddition=num1+num2# printingprint("The sum of the two given numbers is{} ".format(addition))

Output:

Similarly, we can use float() to take two float numbers. Let's see one more example of how to take lists as input

Example 3: Taking Two lists as input and appending them

Taking user input as a string and splitting on each character usinglist() to convert into list of characters.

Python
# Taking list1 input from user as listlist1=list(input("Please Enter Elements of list1: "))# Taking list2 input from user as listlist2=list(input("Please Enter Elements of list2: "))# appending list2 into list1 using .append functionforiinlist2:list1.append(i)# printing list1print(list1)

Output:


input() in Python
Visit Courseexplore course icon
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp