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.

Related Posts

  • How to comment out multiple lines in python
    16 October

    How to comment out multiple lines in python

    Table of ContentsMultiple Line Comments in PythonUsing the # characterUsing triple quotesUsing IDEs and Text Editors In this article, we will see about how to comment out multiple lines in python. The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python print percent sign
    30 September

    Print percentage sign in python

    Table of ContentsIntroductionPrint the Percentage Sign in PythonUsing the %% characterUsing the format() functionUsing the f-string Introduction In this tutorial, we will see how to print percentage sign in python. The modulus operator (%) or the percentage sign has multiple usages in Python. We use this operator to calculate the remainder for the division between […]

  • Print blank line in Python
    23 September

    Print blank line in Python

    Table of ContentsWhat is a blank line in Python?Ways to print blank line in PythonUsing an empty print() functionUsing the print() function with newline characterUsing the print() function with an empty stringUsing the print() function with the sep parameterWays to print multiple blank lines in PythonUsing the print() function with multiple newline charactersUsing the print() […]

  • 17 September

    Write list to CSV in Python

    Table of ContentsUsing the write() function to write a list to a CSV fileUsing the csv module to write a list to a CSV fileUsing the numpy module to write a list to a CSV fileUsing the numpy.savetxt() function to write a list to a CSV fileUsing the numpy.tofile() function to write a list to […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.