Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to input a comma separated string
When working with user input, you often need to process comma-separated strings containing text, numbers, or mixed data. Python provides several approaches to split these strings into individual components and convert them to appropriate data types.
Using split() Function for Text Strings
The split() method is the most common approach to separate comma-delimited text ?
# Input comma-separated string
user_input = "apple, banana, cherry, dates"
print("Original string:", user_input)
# Split by comma and remove extra spaces
items = [item.strip() for item in user_input.split(",")]
print("Processed list:", items)
# Print each item on separate line
for item in items:
print(f"- {item}")
Original string: apple, banana, cherry, dates Processed list: ['apple', 'banana', 'cherry', 'dates'] - apple - banana - cherry - dates
Manual Parsing with Loop
You can manually iterate through the string to identify comma-separated portions ?
user_input = "python, java, javascript, c++"
print("Input string:", user_input)
# Manual parsing
result = []
start = 0
for i, char in enumerate(user_input):
if char == ',':
# Extract substring and remove spaces
item = user_input[start:i].strip()
result.append(item)
start = i + 1
# Don't forget the last item
last_item = user_input[start:].strip()
result.append(last_item)
print("Parsed items:", result)
Input string: python, java, javascript, c++ Parsed items: ['python', 'java', 'javascript', 'c++']
Converting to Integers
When the input contains numeric values, convert strings to integers using int() ?
# Input comma-separated integers
number_input = "10, 25, 30, 45, 60"
print("Input string:", number_input)
# Convert to list of integers
numbers = [int(x.strip()) for x in number_input.split(",")]
print("Integer list:", numbers)
print("Sum:", sum(numbers))
print("Average:", sum(numbers) / len(numbers))
Input string: 10, 25, 30, 45, 60 Integer list: [10, 25, 30, 45, 60] Sum: 170 Average: 34.0
Converting to Float Numbers
For decimal numbers, use float() to convert string values ?
# Input comma-separated decimal numbers
decimal_input = "3.14, 2.71, 1.41, 0.57, 9.81"
print("Input string:", decimal_input)
# Convert to list of floats
decimals = [float(x.strip()) for x in decimal_input.split(",")]
print("Float list:", decimals)
print("Rounded values:", [round(x, 1) for x in decimals])
Input string: 3.14, 2.71, 1.41, 0.57, 9.81 Float list: [3.14, 2.71, 1.41, 0.57, 9.81] Rounded values: [3.1, 2.7, 1.4, 0.6, 9.8]
Comparison of Methods
| Method | Best For | Advantages |
|---|---|---|
split() + strip()
|
Most cases | Simple, readable, handles spaces |
| List comprehension | Type conversion | Concise, handles conversion in one line |
| Manual parsing | Complex logic | Full control over parsing process |
Conclusion
Use split(",") with strip() for most comma-separated string processing. Combine with list comprehension for type conversion. The strip() method ensures clean data by removing unwanted whitespace.
