User Input - Python Programming Language

There are hardly any programs without any input. Input can come in various ways, for example from a database, another computer, mouse clicks and movements or from the internet. Yet, in most cases the input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string.

If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The text of the optional parameter, i.e. the prompt, will be printed on the screen.

The input of the user will be returned as a string without any changes. If this raw input has to be transformed into another data type needed by the algorithm, we can use either a casting function or the eval function.

For example:

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So,you are already "+str(age)+" years old, "+name+"!")

Output:

What's your name? Aniket
Nice to meet you Aniket!
Your age? 19
So,you are already 19 years old, Aniket!

For declaration different data type for input:

Example:

name=str(input("Enter your name: "))
roll=int(input("Enter your roll no: "))
mark=float(input("Enter your marks: "))
print("Your name is "+name)
print("Your roll  no. is ",+roll)
print("Your marks is ",+mark)

Output:

Enter your name: Divik
Enter your roll no: 15110620
Enter your marks: 73.6
Your name is Divik
Your roll no. is 15110620
Your marks is 73.6

Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.