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.
Table of Contents
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:
1 2 3 4 5 |
lang = 'Python,Java,C,C++,Golang' print("Original String: "<strong>, </strong>lang) # Some Method That Splits the string by comma |
Expected Output:
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)
Parameter | Description |
separator | It 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. |
maxsplit | This 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
1 2 3 4 5 6 |
lang = 'Python,Java,C,C++,Golang' print("Original String: ", lang) print("Splitted Elements: ", lang.split(',')) |
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:
1 2 3 4 5 6 |
lang = 'Python,Java,C,C++,Golang' print("Original String: ", lang) print("Splitted String: ", [u for x in lang.split(',') for u in (x,',')]) |
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).
1 2 3 4 5 6 7 8 |
import re #importing regular expressions text = "This,,is,,a,,comma,separated,string" print("Unnecessary Splits: ",text.split(',')) print("Getting rid of unnecessary splits with regex:") print("Splitted string: ",re.split(',+',text)) # ‘,+’ is the regular expression for one or more commas (separators) |
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:
1 2 3 4 5 6 |
lang = 'Python,Java,C,C++,Golang' print("Original String: ", lang) lang = lang.replace(',','\n') print("Splitted String: ", lang.splitlines()) |
Output:
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:
1 2 3 4 5 |
sample=30313031303 print(sample.split('1')) |
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.
1 2 3 4 5 |
sample = 30313031303 print(str(sample).split('1')) |
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.