How to Solve Python AttributeError: ‘int’ object has no attribute ‘split’

by | Programming, Python, Tips

This error occurs if you try to call the split() method on an integer. The split() method belongs to the string class and splits a string using a delimiter returning a list of strings.

You can solve this error by checking the type of the object before calling the split() method to ensure that the object is a string.


AttributeError: ‘int’ object has no attribute ‘split’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. split() is a string method, which splits a string into a list of strings using a separating character. We pass a separating character to the split() method when we call it.

Example: Splitting an Integer

Let’s look at an example of an integer which we want to split into three numbers. We will attempt to call the split() method on the integer:

# Define integer

my_int = 246

# Call split() method

split_numbers = my_int.split()

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [5], in <cell line: 5>()
      1 # Define integer
      3 my_int = 246
----> 5 split_numbers = my_int.split()

AttributeError: 'int' object has no attribute 'split'

The error occurs because the split() method belongs to the string class and my_int is an object of the integer class. We can check what attributes an object has by using the dir() function, for example:

print(dir(my_int))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

The dir() function returns a list of all the attributes that an object has. We can use the in operator to search for a specific attribute instead of looking by eye.

If the in operator returns False then the attribute does not exist for that object. Let’s see if split is in the list:

print('split' in dir(my_int))
False

The membership operation returns False, verifying that split is not an integer attribute.

Solution: Convert Int to String using str

We can solve this error by converting the integer to a string using the str() function. Then we can call the split() method on the newly converted string. Let’s look at the revised code:

# Define integer

my_int = 246

my_int = str(my_int)

result = my_int.split()

print(result)

Let’s run the code to see the result:

['246']

The split() method returns a list containing the string '246'. The default separator for the split() method is whitespace. There is no whitespace in the string. Therefore, the string is left intact. We pass an empty separator to the split() method.

If we want to split a string into characters, we can use the built-in list() function as follows:

split_numbers = list(my_int)

print(split_numbers)
['2', '4', '6']

Then if we’re going to convert the characters into integers, we can use a list comprehension with the built-in int() function as follows:

split_numbers = [int(ch) for ch in split_numbers]
print(split_numbers)
[2, 4, 6]

We successfully obtained a list containing the three numbers that made up the initial integer.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee