VOOZH about

URL: https://www.geeksforgeeks.org/python/python-splitfields-method/

⇱ Python splitfields() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python splitfields() Method

Last Updated : 23 Jul, 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:

  • 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

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

Output:

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

Example 3: Using splitfields() with a List

Output:

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

Example 4: Using splitfields() with a Set

Output:

['quick', 'brown', 'fox', 'The']
['apple banana orange'] 
Comment
Article Tags: