How do I Input a String From the User in Python?


In Python, there are several ways to input a string from the user. The most common method is by using the built-in function input(). This function allows the user to enter a string, which is then stored as a variable for use in the program.

Example

Here's an example of how to input a string from the user in Python −

# Define a variable to store the input
name = input("Please enter your name: ")
# Print the input
print("Hello, " + name + "! Good to see you.")

Output

The above code generates the following output for us −

Please enter your name: Max
Hello, Max! Good to see you.

In the code above, we have,

  • Define a variable to store the input − name = input("Please enter your name: ")

    • In this step, a variable named "name" is created to store the input from the user.

  • Prompt the user to enter their name − input("Please enter your name: ")

    • The "input()" function is used to display a message to the user, asking them to enter their name. The message, "Please enter your name: ", is passed as an argument to the function.

  • Store the user's input in the "name" variable − name = ...

    • The result of the "input()" function call is stored in the "name" variable. This means that the user's input is now stored in the "name" variable, ready to be used.

  • Print the input − print("Hello, " + name + "! Good to see you.")

    • In this step, the "print()" function is used to display a message to the user, using the value stored in the "name" variable. The message, "Hello, [name]! Good to see you.", is passed as an argument to the function. The value of "name" is concatenated with the rest of the string using the "+" operator.

It's crucial to remember that the output of the "input()" function will always be a string, even if the user enters a numerical value. If you need to use the input as a number, you'll need to convert it to the appropriate data type (e.g. int or float).

Example

Here's an example of how to input a number from the user −

# Define a variable to store the input
age = int(input("Please enter your age: "))
# Print the input
print("Wow, you are " + str(age) + " years old!")

Output

The above code generates the following output for us −

Please enter your age: 24
Wow, you are 24 years old!

From the above code,

  • A variable named "age" is created to store the input from the user.

  • The message, "Please enter your age: ", is passed as an argument to the function.

  • Since the "input()" function always returns a string, we need to convert the user's input to an integer using the "int()" function. This allows us to store the user's input as a number, rather than a string.

  • The result of the "int()" function call is stored in the "age" variable.

  • The "print()" function is used to display a message to the user, using the value stored in the "age" variable. The message, "Wow, you are [age] years old!", is passed as an argument to the function. The value of "age" is first converted to a string using the "str()" function and then concatenated with the rest of the string using the "+" operator.

It's also possible to assign a default value to the input, in case the user doesn't provide any input. This can be done using the "or" operator and a default value −

Example

# Define a variable to store the input
name = input("Please enter your name (or press enter for default): ") or "Max"
# Print the input
print("Hello, " + name + "! Good to see you.")

Output

The above code generates the following output for us −

Please enter your name (or press enter for default): 
Hello, Max! Good to see you.

Here in the code above,

  • A variable named "name" is created to store the name input by the user.

  • The message, "Please enter your name (or press enter for default) − ", is passed as an argument to the function.

  • The or operator is used to set a default value for the name variable. If the user presses enter without entering a name, the input() function will return an empty string. If the user's input is an empty string, the or operator will evaluate to the default value, "Max".

  • The result of the input() function call, or the default value "Max" is stored in the name variable.

  • A personalized greeting is printed, using the name variable. The + operator is used to concatenate the string values, creating a single string to be printed.

Conclusion

To summarize, receiving a string from a user in Python is a simple task that can be accomplished by making use of the readily available "input()" method. Regardless of whether you need to collect a string or a numerical value, it is effortless to convert the input into a suitable data type and save it in a variable for future reference.

The "input()" method is a convenient tool for obtaining information from a user and storing it for later use in your code.

Updated on: 31-Oct-2023

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements