Python splitfields() Method (original) (raw)

Last Updated : 24 Apr, 2025

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:

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 "", line 2, in 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']