Python program to input a comma separated string


When a text string is entered or given as input, it may have commas in between. Sometimes the task is to separate all the comma-separated portions of a sentence or a text string. These portions may have single word or multiple words. These string portions may be further entered as items of list or can be processed further. Similarly, numbers in integer form or decimal form are also needed to be entered while being separated by commas. In such cases, understanding these as numbers is important. By using four different examples, this process of given comma separated string or sentence, or numbers and processing it by understanding its comma separated structure by a Python program is demonstrated in this article.

Example 1 - A program to input a comma separated string and find the comma separated portions using the split function

Algorithm

Step 1 − First enter a string that is separated by commas.

Step 2 − Use the split function to separate the comma-separated portions into a list.

Step 3 − Remove the empty spaces from the left of the list items.

Step 4 − Remove the empty spaces from the right of the list items.

Step 5 − Run the program and then check the result.

The Python File Contains this

commaSepStr = input ("Enter a comma separated String:")
list1 = commaSepStr.split(",")

def removeLspace(list):
   return [item.lstrip() for item in list]
    
print(commaSepStr)
print(list1)

def removeRspace(list):
   return [item.rstrip() for item in list]

noextraleftspace_list = removeLspace(list1)
noextrarightspace_list = removeRspace(noextraleftspace_list)

print(noextrarightspace_list)
print(*noextrarightspace_list, sep = "\n")

Viewing The Result - Example 1

For seeing the result run the Python file in the cmd window.

Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
['Our last night plate included two rotis', 'daal', 'mixveg', ' rice', ' paneer', ' salad and achaar']
['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar']
Our last night plate included two rotis
daal
mixveg
rice
paneer
salad and achaar

Example 2: A program to input a comma separated string and find the comma separated portions using a ‘for’ loop.

Algorithm

Step 1 − First give an input string that is separated by commas.

Step 2 − Go through the string character by character and identify the comma separated portions and append these to a list.

Step 3 − Remove the empty spaces from the left of the list items.

Step 4 − Print the list having items without extra empty spaces.

Step 5 − Run the program and then check the result.

The Python File Contains this

commaSepStr = input ("Enter a comma separated String :")

print("The Entered String is: " + commaSepStr)
 
startofItem = 0
list1=[]
for item in range(len(commaSepStr)):
   if commaSepStr[item] == ',':
      # characters from startofItem to comma
      nospaceitem=commaSepStr[startofItem:item].lstrip()
      list1.append(nospaceitem)
      startofItem = item+1
      print(nospaceitem)

# characters from startofItem to end
nospaceitem=commaSepStr[startofItem:].lstrip()        
print(nospaceitem)
list1.append(nospaceitem)
print(list1))

Viewing the Result

Open the cmd window and run the python file to see the result.

Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
The Entered String is: Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
Our last night plate included two rotis
daal
mixveg
rice
paneer
salad and achaar
['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar']

Example 3 - A program to input a comma separated string having integers

Algorithm

Step 1 − First enter a string that is separated by commas and contains only integers.

Step 2 − Use the split function to separate the comma-separated integers into a list of strings.

Step 3 − Take each item from this string list and convert these to integer type and append these to another list as integers.

Step 4 − Run the program and then check the result.

The Python File Contains this

# input comma-separated numbers as string 
strInput = input ("Enter comma separated integers: ")
print( "Input string: ", strInput)

# convert to the list
strlist = strInput.split(",")
print("list of string type numbers: ", strlist)

# convert each string element as integers
list1 = []
for item in strlist:
	list1.append(int(item))

# print list as integers
print("list of integers: ", list1)

Viewing The Result - Example 3

For seeing the result run the Python file in the cmd window.

Enter comma separated integers: 101, 280, 98, 185, 934, 9684, 955, 20, 34
Input string:  101, 280, 98, 185, 934, 9684, 955, 20, 34
list of string type numbers:  ['101', ' 280', ' 98', ' 185', ' 934', ' 9684', ' 955', ' 20', ' 34']
list of integers:  [101, 280, 98, 185, 934, 9684, 955, 20, 34]

Example 4: A program to input a comma separated string having decimal numbers

Step 1 − First enter a string that is separated by commas and contains only integers and decimal numbers.

Step 2 − Use the split function to identify the comma-separated numbers and append these into a list as strings.

Step 3 − Take each number from this string list and convert these to float type and append these to another list as decimals.

Step 4 − Run the program and then check the result.

The Python File Contains this

# input comma separated numbers as string 
strInput = input ("Enter comma separated numbers: ")
print( "Input string: ", strInput)

# convert to the list
strlist = strInput.split (",")
print("list of string type numbers: ", strlist)

# convert each string element as integers
list1 = []
for item in strlist:
	list1.append(float(item))

# print list as integers
print("list of decimal numbers: ", list1)

Viewing the Result - Example 4

Open the cmd window and run the python file to see the result.

Enter comma-separated numbers: 102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009
Input string:  102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009
list of string type numbers:  ['102.88', ' 6.5', ' 6767.907', ' 5555.3', ' 4545', ' 6677', '56.009']
list of decimal numbers:  [102.88, 6.5, 6767.907, 5555.3, 4545.0, 6677.0, 56.009]

Fig 4: Showing the list with comma-separated portions from the input string having decimal numbers.

In this Python article, using four different examples, the ways to show how to input a comma-separated string are given. First, in example 1, the parts of the string are separated by a comma by using the split function. In example 2, the comma-separated portions are identified by going through the string by checking all characters. In example 3, integers are input as strings and in example 4, decimal numbers are given as input strings and then separated into a list.

Updated on: 28-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements