Python allows interaction with users to get data or provide a result. Most applications use a dialog box as a form of user input and Python provides raw_input() and input() as two inbuilt functions. However, for this Python tutorial we will use input() because we are using Python 3.7.
Python input() function
The Python input() function allows user input by prompting the user with a statement to provide input. The syntax is input(prompt) and the prompt is a string representing a statement message before the input.
Python input() Function Example
The below example will ask a user to provide details with first and last name, age, city, state and email. Notice the input for age has int(input()) and will only accepts numbers into the variable.
Python Code Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | print("Please provide the following information to sign-up") print("-"*52) first_name = input("\nWhat is your first name: ") last_name = input("What is your last name: ") age = int(input("How old are you: ")) city_location = input("What city do you live in? ") state_location = input(f"What state is {city_location} in? ") email = input("Please provide your email address: ") print(f"Thank you for providing the below information {first_name} {last_name}.") print(f"Age: {age}") print(f"City: {city_location}") print(f"State: {state_location}") print(f"Email: {email}") |
Python User Input:
1 2 3 4 5 6 | What is your first name: Bill What is your last name: Armstrong How old are you: 40 What city do you live in? San Jose What state is San Jose in? California Please provide your email address: billarmstrong40@gmail.com |
Python Code Output:
1 2 3 4 5 | Thank you for providing the below information Bill Armstrong. Age: 40 City: San Jose State: California Email: billarmstrong40@gmail.com |