Python Split String by Comma

Outline: For splitting any string, Python provides us with a predefined function known as split(). Use given_string.split(',') to split the string by comma.

Introduction

If you want to separate a string with commas manually, it may consume a lot of time. Especially when a string is lengthy, it becomes quite difficult to split it manually. Luckily in Python, we have a solution for it.

Example:

Expected Output:

Original String: Python,Java,C,C++,Golang
Splitted Elements: [‘Python’, ‘Java’, ‘C’, ‘C++’, ‘Golang’]

Thus, in this article, you will learn about the methods that will allow you to split a string using a comma as the separator.

📜 Method 1: Using split() Method

split() is a built-in method in Python that is used to separate a string based on a given separator/delimiter.

Syntax:

string.split(separator, maxsplit)

ParameterDescription
separatorIt is a parameter of the split() method that informs Python where the string is to be broken/separated from. A separator can be any character like comma, space etc.
maxsplitThis is an optional parameter that allows you to provide a valid number up to which you want to split the string. If you pass 0 then there will be no split, and the function will return the entire string in a list. By default, the value is -1, which is “all occurrences”.

Splitting a String Using The split() Method with Comma as The Separator

Output:

Original String: Python,Java,C,C++,Golang
Splitted Elements: [‘Python’, ‘Java’, ‘C’, ‘C++’, ‘Golang’]

Explanation: In the above example, the split() method helped us to split the string whenever a comma occurred since the separator specified within the split() function was comma(,).

📜 Method 2: Using split() and a List Comprehension

You can use a list comprehension along with the split() method to split the string by comma. This is an effective solution in case you not only want to split the string but also want to keep the separators (comma in this case).

Example:

Output:

Original String: Python,Java,C,C++,Golang
Splitted String: [‘Python’, ‘,’, ‘Java’, ‘,’, ‘C’, ‘,’, ‘C++’, ‘,’, ‘Golang’, ‘,’]

📜 Method 3: Using regex.split()

You can use the split() method within the regex module of Python to split the string based on a given separator. The advantage of this method that it allows you to fix the unnecessary splits.

For example: Consider we have the following string with an unnecessary comma within it. Here, the regex module will help us to get rid of this unnecessary character (comma).

Output:

Unnecessary Splits: [‘This’, ”, ‘is’, ”, ‘a’, ”, ‘comma’, ‘separated’, ‘string’]
Getting rid of unnecessary splits with regex:
Splitted string: [‘This’, ‘is’, ‘a’, ‘comma’, ‘separated’, ‘string’]

Explanation:

  • In the above example, you can see that the inconsistency in separators in the string causes unnecessary splits in the list and results in empty strings when we use the normal split() method.
  • To fix the unnecessary splits, we need to import the re module, i.e. regular expressions module in Python. After which, we need to use the re.split() method.

📜 Method 4: Using replace() + splitlines()

Another approach to solving our problem is to use the replace() method to replace the comma with a newline character, and then you can split the newly formed string with the splitlines() method.

 💡 Note:

  • splitlines() is a built-in method in Python which enables you to split a string based on the line breaks.
  • replace() is another built-in method in Python which allows you to replace a certain character or substring with another character or substring.

The Solution:

Output:

Original String: Python,Java,C,C++,Golang
Splitted String: [‘Python’, ‘Java’, ‘C’, ‘C++’, ‘Golang’]

🦟 Attribute error: ‘int’ object has no attribute ‘split’

While using the split() method, a common error that you might come across is Attribute error: 'int' object has no attribute 'split'.

Example:

Output:

Traceback (most recent call last):
File “D:/PycharmProjects/PythonErrors/rough.py”, line 2, in
print(sample.split(‘1’))
AttributeError: ‘int’ object has no attribute ‘split’

The above error is due to the incorrect type of attribute given as input to split(). Here ‘int’ object has no attribute named split(), so it gives an attribute error. The same attribute error may be caused with any object other than str.

Solution/Fix: If you want to forcefully split any object then you can do it with the help of typecasting.

Output:

[‘303’, ‘303’, ‘303’]

🏋️ Exercise

Before wrapping up this article, here’s an exercise to test your understanding of the above concept.

Given strings:

fruits = ‘apple,mango,orange,watermelon,cherry’
mixed = ‘[email protected]

Desired Output:

Splitted Fruits: [‘apple’, ‘mango’, ‘orange’, ‘watermelon’, ‘cherry’]
Spllited Mixed Text: [‘shubham’, ‘sayon’, ‘test.com’]

#HINT: Use the split() and re.split() methods.

Solution:

Conclusion

I hope this article answered all your queries, and now you can split a string by comma like a pro. Please subscribe and stay tuned for more interesting articles. Happy coding! 📚

📝 This article was written by Shubham Sayon and Anirban Chatterjee.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.