Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tutorial - Learn Python Programming Language
Next article icon

In Python, we use the input()function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.

Python input() Function Syntax

Syntax: input(prompt)

Parameter:

Return:String object

How input() Function works in Python?

In this example, we are using input() function to input user data as a string in Python.

Python
name=input("What is your name? ")print("Hello, "+name+"!")

Output

What is your name? GFG
Hello, GFG!

input() Function in Python Examples

Taking input in Python

In this example, we are using the Python input() function to input user data as a string inPython, which takes input from the user and prints it.

Python
# Taking input from the userstring=input()# Outputprint(string)

 Output

geeksforgeeks

User Input in Python

In this example, we are taking input from the user and input user data as a string inPythonwith a prompt and printing it.

Python
# Taking input from the username=input("Enter your name")# Outputprint("Hello",name)

 Output

Enter your name:ankit rai
Hello ankit rai

Convert User Input to a Number

In this example, we are using the Python input() function which takes input from the user in string format converting it into an integer adding 1 to the integer, and printing it.

Python
# Taking input from the user as integernum=int(input("Enter a number:"))add=num+1# Outputprint(add)

Output

Enter a number:15
16

Take float input in Python

In this example, we are using the Python input() function which takes input from the user in string format converts it into float adds 1 to the float, and prints it.

Python
# Taking input from the user as floatnum=float(input("Enter number "))add=num+1# outputprint(add)

Output

Enter number 5
6.0

Python Accept List as a input From User

In this example, we are taking input from the user in string format converting it into alist, and printing it.

Python
# Taking input from the user as listli=list(input("Enter number "))# outputprint(li)

Output

Enter number 12345
['1', '2', '3', '4', '5']

Take User Input for Tuples and Sets

In this example, we are taking input from the user in string format converting it into atuple, and printing it.

Python
# Taking input from the user as tuplenum=tuple(input("Enter number "))# outputprint(num)

Output

Enter number 123
('1', '2', '3')

Input with a dictionary comprehension

In this example, we are taking the words separated by space to input user data as a string inPython, and we make adictionaryof the word as the key with their length as the value.

Python
words_str=input("Enter a list of words, separated by spaces: ")words={word:len(word)forwordinwords_str.split()}print(words)

Output

Enter a list of words, separated by spaces: geeks for geeks
{'geeks': 5, 'for': 3}

Improve
Practice Tags :

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