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

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Sleep for milliseconds in python
    31 May

    How to sleep for milliseconds(ms) in Python

    Table of ContentsUse time.sleep() function to make the program sleep for millisecondsUse threading.Timer() function to make the program sleep for milliseconds 💡 Outline You can use time.sleep() method to sleep for milliseconds in Python. If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method. […]

  • Convert unix timestamp to datetime in Python
    21 May

    Convert Unix timestamp to DateTime in python

    Table of ContentsUsing fromtimestamp() FunctionUsing strftime() FunctionUsing to_datetime() Function A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC. In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python. Using fromtimestamp() […]

  • Python round to 2 decimal places
    21 May

    How to round to 2 decimals in Python

    Table of ContentsUsing the round() functionUsing the format() functionUsing the % formattingUsing the f-stringsUsing the math.ceil() and math.floor() functions In Python, we deal with integers, floating values, and even imaginary numbers. Float numbers are used to represent the real numbers with integers and decimal parts. Python represents such numbers with 64-bit precision giving it a […]

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.