Open In App

How to take string as input from a user in Python

Last Updated : 26 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Accepting input is straightforward and very user-friendly in Python because of the built-in input() function. In this article, we’ll walk through how to take string input from a user in Python with simple examples.

The input() function allows us to prompt the user for input and read it as a string. By default, whatever we type in the input prompt is stored as a string. Here’s how we can use it:

n = input("Enter your name: ")

print("Hello,", n)

Explanation: The input("Enter your name: ") line displays a prompt for the user to enter their name. The entered string is stored in the variable name. print("Hello,", name) outputs the entered string along with the greeting.

Let's take a look at other cases of taking string as input:

Taking Input Splits, Space-Separated Strings

When taking string input from users, you might need to process the input in various ways, such as splitting it by default spaces, handling space-separated input, or using a custom delimiter for separation. Here, we'll cover all these methods with a single example.

# Taking input from the user
s1 = input("Enter your input (e.g., 'apple banana cherry' or 'apple,banana,cherry'): ")

# 1. Default split (splits by whitespace)
sp = s1.split()
print("Default split (by whitespace):", sp)

# 2. Space-separated input
ss = s1.split(' ')
print("Split by space:", ss)

# 3. Custom delimiter input (comma-separated in this case)
sc = s1.split(',')
print("Split by comma:", sc)

Output:

Enter your input (e.g., 'apple banana cherry' or 'apple,banana,cherry'): apple banana cherry, abc efg
Default split (by whitespace): ['apple', 'banana', 'cherry,', 'abc', 'efg']
Split by space: ['apple', 'banana', 'cherry,', 'abc', 'efg']
Split by comma: ['apple banana cherry', ' abc efg']

Explanation: split() without arguments breaks the string based on whitespace. split(' ') specifically targets spaces as delimiters. split(',') demonstrates splitting the input string based on a custom delimiter (comma).

Note: You can add delimiters in spilt() to take input in the way you want to take from the user. It will split the string by the given delimiter.

Taking Multiple String Inputs

To accept multiple string inputs, we can use multiple input() calls. This way we can accept and manipulate multiple pieces of data entered by the user.

f = input("Enter your first name: ")

l = input("Enter your last name: ")

print("Your full name is:", f, l)

Here, we prompt the user twice to input their first and last names separately. We print the combined result.

Converting Input to Other Data Types

Although input() always returns a string, we can easily convert the input to other types using functions like int(), float(), or bool(). For example:

age = input("Enter your age: ")

# Convert string to integer
#If the user enters something that is not a valid integer (e.g., a letter), it will raise a ValueError.

age = int(age)

print("In 10 years, you will be", age + 10, "years old.")

The input() function reads user input as a string. The int() function converts the string to an integer. The program then adds 5 to the user's age and prints a future age calculation.


Similar Reads

three90RightbarBannerImg