AppDividend
Latest Code Tutorials

Python Input Example | Python input() | Get Input From User

0

Python input method reads a line from input, converts into a string and returns it. Python input() function allows user input. If we want to capture the input in the program, we will need a variable. The variable is a container to hold data.

Python Input Example

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. 

You can take input and assign it to the variable. It is done using the = operator before the input keyword and then putting the variable name before the = operator.

The syntax for the input() method is the following.

input(prompt)

The input() method takes a prompt as an optional argument. The prompt String, representing a default message before the input. If EOF is read, it raises an EOFError exception. 

One thing to that, if you are using Python 2.x then you have to write:

x = raw_input()

raw_input( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store.

See the following example. We are using Python 3.0 so we only need input() function.

# app.py

surname = input('What is your surname? ')
print('Your surname is: ',surname)

The output of the method is following.

Python Input Example

Example 2:

Let’s see the following example.

# app.py

inputString = input()
print('The inputted string is:', inputString)

See the output below.

Getting Input From User Tutorial

The above example is not right in the sense of User Experience. It is often the right idea to tell the user what to input.

We can do this by putting the hint in quotes inside the input parentheses. That hint will come in the next line and will wait for the user to input. 

Then, the user can then type the input, and when you hit the ENTER key, it will capture the input.

Finally, Python Input Example | Getting Input From User Tutorial is over.

Recommended Posts

Python hex() example

Python next() example

Python ord() example

Python sqrt() example

Python abs() example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.