Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python input() Function
Next article icon

In Python, both input() andraw_input() functions are used to take user input.

Note: raw_input()was used inPython 2but is no longer available inPython 3, as it has been deprecated.

Let’s understand the difference clearly.

input() function

Theinput()function is used to take input from the user as a string. It waits for the user to type something and press Enter, then returns that input as a string which can be stored or processed in the program. 

Example program in Python3 

Python
val1=input("Enter the name: ")print(type(val1))print(val1)val2=input("Enter the number: ")print(type(val2))val2=int(val2)print(type(val2))print(val2)

Input and Output

Here, the value "python3" take from the user and store it in theval1variable. The type of the value stored is always string for input function only for Python 3.x. The value "1997" take from the user and store it in the variableval2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int() function. The val2 variable stores the value "1997" as an integer type. 

Example program in Python2 

Python
# Python program to demonstrate# input() function in Python2.xval1=input("Enter the name: ")print(type(val1))print(val1)val2=input("Enter the number: ")print(type(val2))print(val2)

Input and Output

 Here, the value "python3" take from the user and store it in theval1 variable. The function takes the value and type of the input you enter as it is without modifying the type. The type of value in val1 is string type. The value "1997" takes from the user and store it in the variableval2. Now, the type of variable val2 is integer type. We don't need to explicitly change the variable type.

raw_input() function

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function. The input function isused only in Python 2.x version. The Python 2.x has two functions to take the value from the user. The first one is input function and another one is raw_input() function. The raw_input() function is similar to input() function in Python 3.x. Developers are recommended to use raw_input function in Python 2.x.

Example program in Python2 

Python
val1=raw_input("Enter the name: ")print(type(val1))print(val1)val2=raw_input("Enter the number: ")print(type(val2))val2=int(val2)print(type(val2))print(val2)

Input and Output

  

Here, the value "python3" take from the user and store it in theval1variable. The type of the value stored is always string for raw_input function. The value "1997" take from the user and store it in the variable val2. Now, the type of variableval2is a string and we have to convert the type to an integer using int() function. The val2 variable stores the value "1997" as an integer type.

Let us see the differences in a tabular form -:

 input() raw_input() 
1.input() function take the user input.raw_input() function takes the input from the user.
2.

Its syntax is -:

input(prompt)

Its syntax is -:

raw_input(input)

3.It takes only one parameter that is prompt.It takes only one parameter that is the input.
4.It return the input that it takes.Its return type is of string.
5.It converts the input into a string by removing the trailing newlineIt is only introduced in python 2.0 version
6.blocks until input receivedhang 'till user inputs
7."hello world""hello world" but string
8.fooin snake_case

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