How to split a string by comma in Python

We will learn how to split a string by comma in Python, which is a very common task in data processing and analysis. Python provides a built-in method for splitting strings based on a delimiter, such as a comma.

Splitting a string by comma is a fundamental operation in data processing and analysis using Python. Whether you’re working with a CSV file or a string that contains a list of values, being able to split the string based on a delimiter like a comma is essential. Python provides a built-in method called split() for this purpose, which can be used to split a string into a list of individual values.

In this article, we will explore various examples of how to split a string by comma in Python and demonstrate practical use cases for this operation. By the end of this article, you will have a solid understanding of how to use the split() method to process and analyze data in Python.

Example-1: Basic String Splitting by Comma

Let’s start with a simple example. Suppose we have a string containing a list of names separated by commas, and we want to split this string into a list of individual names:

names = "John, Jane, Bob, Alice"
name_list = names.split(",")
print(name_list)

Output:

['John', ' Jane', ' Bob', ' Alice']

As you can see, we used the Python split() method of the string object, passing a comma as the argument. This returns a list of the individual names. Note that the spaces between the names are also included in the resulting list.

Example-2: split a string by comma using strip() method

Here is another example of how to split a string by comma using strip() method in Python.

names = "John, Jane, Bob, Alice"
name_list = [name.strip() for name in names.split(",")]
print(name_list)

Output:

['John', 'Jane', 'Bob', 'Alice']

In this example, we used a list comprehension to iterate over the names in the list returned by split(). For each name, we used the Python strip() method to remove the leading and trailing spaces.

Example-3: Splitting a CSV File

Now let’s consider a more practical example. Suppose we have a CSV (Comma-Separated Values) file containing data in the following format:

Name, Age, Gender
John, 25, Male
Jane, 30, Female
Bob, 40, Male
Alice, 35, Female

We can use Python to read this file and split each line into a list of values:

with open("data.csv") as f:
    for line in f:
        values = line.strip().split(",")
        print(values)

Output:

['Name', ' Age', ' Gender']
['John', ' 25', ' Male']
['Jane', ' 30', ' Female']
['Bob', ' 40', ' Male']
['Alice', ' 35', ' Female']

In this example, we used the open() function to open the file, and then we used a for loop to iterate over the lines in the file. For each line, we used strip() to remove any leading or trailing spaces, and then we used split() to split the line into a list of values based on the comma delimiter.

How to split a string by comma in Python
How to split a string by comma in Python

Conclusion

In this blog post, we learned how to split a string by comma in Python using the built-in split() method. We also saw some examples of how to use this method in practical situations, such as processing CSV files.

You may also like: