Open In App

Python splitfields() Method

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The splitfields() method is a user-defined method written in Python that splits any kind of data into a list of fields using a delimiter. The delimiter can be specified as an argument to the method, and if no delimiter is specified, the method splits the string using whitespace characters as the delimiter.

Syntax: string.splitfields(delimiter)

Parameters:

  • string (required) – The string to be split into fields.
  • delimiter (optional) – The character or string to use as the delimiter for splitting the string into fields. If this parameter is not specified, the method uses whitespace characters as the delimiter.

Return Value: The splitfields() method returns a list of fields that were separated by the specified delimiter. If no delimiter is specified, the method splits the string using whitespace characters. If the string is empty, the method returns an empty list.

Type Error: If you call the splitfields() method with the wrong number or type of arguments, you will get a TypeError. 

Examples of splitfields() Method

Let’s see some examples of how can a user-defined splitfields() Method can be implemented in Python.

Example 1: Using splitfields() with the wrong type of arguments

Python3




# Example of a TypeError when calling splitfields()
num = 123
fields = num.splitfields(",")


Output:

Since the splitfields() method can only be called on string objects, this will result in a TypeError. The error message will look something like this:

Traceback (most recent call last):
 File "<ipython-input-1-6b9c6a2fbfd8>", line 2, in <module>
   fields = num.splitfields(",")
AttributeError: 'int' object has no attribute 'splitfields'

Example 2: Using splitfields() with a String

Python3




class MyString(str):
    def splitfields(self, sep=None):
        if sep is None:
            return self.split()
        else:
            return self.split(sep)
  
# Splitting a string into fields using whitespace as delimiter
str1 = "The quick brown fox"
fields1 = MyString(str1).splitfields()
print(fields1)
  
  
# Splitting a string into fields using a specific delimiter
str2 = "apple,banana,orange"
fields2 = MyString(str2).splitfields(",")
print(fields2)


Output:

['The', 'quick', 'brown', 'fox']
['apple', 'banana', 'orange']

Example 3: Using splitfields() with a List

Python3




class MyString(str):
    def splitfields(self, sep=None):
        if sep is None:
            return self.split()
        else:
            return self.split(sep)
  
# Splitting a list into fields using whitespace as delimiter
lst1 = ["The", "quick", "brown", "fox"]
fields3 = MyString(" ".join(lst1)).splitfields()
print(fields3)
  
# Splitting a list into fields using a specific delimiter
lst2 = ["apple", "banana", "orange"]
fields4 = MyString(",".join(lst2)).splitfields(",")
print(fields4)


Output:

['The', 'quick', 'brown', 'fox']
['apple', 'banana', 'orange']

Example 4: Using splitfields() with a Set

Python3




class MyString(str):
    def splitfields(self, sep=None):
        if sep is None:
            return self.split()
        else:
            return self.split(sep)
  
class MySet(set):
    def splitfields(self, sep=None):
        str_set = " ".join(self)
        return MyString(str_set).splitfields(sep)
  
# Splitting a set into fields using whitespace as delimiter
set1 = {"The", "quick", "brown", "fox"}
fields5 = MySet(set1).splitfields()
print(fields5)
  
  
# Splitting a set into fields using a specific delimiter
set2 = {"apple", "banana", "orange"}
fields6 = MySet(set2).splitfields(",")
print(fields6)


Output:

['quick', 'brown', 'fox', 'The']
['apple banana orange'] 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads