Krivalar Tutorials 
Krivalar Tutorials

Python - Input() function, raw_input() function - Syntax & Examples



<Membership Operators    Output >







We want the user to interact with our program.

Python provides two built-in input functions

  • The input() function----python 3.6 version.
  • The raw_input() function----python 2.x version.

The input() function is used to get the input data from the users and convert it into a string.

When the input() function is invoked, python stops the program execution and waits for the user to give the input data. After receiving the input data, python will continue the execution and display what the user entered on the screen.

Syntax for Python-Input

Following is the basic syntax for python input() function.

Variable_name = input(prompt)

Variable_name = raw_input(prompt)
  • The prompt is an optional string that will be displayed on the screen.
  • The prompt is indicating the user that keyboard input is required and waits for the user to type input data through a keyboard.
  • After receiving, the function treats the input as a string.
  • If you enter an integer value, the input() function will convert it into a string. For this integer's input, you need to use typecasting to convert it into an integer in your program.
Note:

The raw_input() function works in an older version of python( python 2. x).

Example using Python Input

Following is the simple program to get information from user.

Name=input('Enter your Name:')
Age=input('Enter yuor Age:')
print(Name)
print(Age)

While executing the above program, you will be asked to give the name and age details of the user via the keyboard.

Output

While executing the above program, you will be asked to give the name and age details of the user via the keyboard.

F:\>python user.py
Enter your Name:Anu
Enter your Age:35
Anu
35

Another Example

>> Name=input('Enter your name:')
Enter your name: Harish
>> Age=input("Enter your age:")
 Enter your age:36
>> print("Name = "+Name)
Name = Harish
>> print("Age = "+Age)
Age = 36

<Membership Operators    Output >





















Searching using Binary Search Tree