[Solved] Python, A string split into many pieces. How many pieces are in there?


You could get the length of the list of items after splitting:

len(my_long_string.split(';'))

Alternatively, count the number of semicolons and add one:

my_long_string.count(';') + 1

The latter is probably faster if you don’t need to know what the items are, but only how many items there are.

len is a function that returns the number of items in a list. Strings have a method called split that splits a string on a delimeter. They also have a method called count that counts the number of non-overlapping instances of a substring.

1

solved Python, A string split into many pieces. How many pieces are in there?