To get a substring from a list element in Python, first access the item by index, then slice the string. For example: my_list[0][1:4] returns characters from position 1 up to, but not including, 4.

Common cases

  • Get one item from the list: my_list[2]
  • Get part of that string: my_list[2][0:3]
  • Get from a position to the end: my_list[2][4:]

Example:

python

my_list = ["apple", "banana", "cherry"]
print(my_list[1][0:3])   # ban
print(my_list[2][2:])    # erry

If you meant searching for a substring

If your goal is to find list items containing a substring, use a list comprehension like this: [s for s in my_list if sub in s]. That returns all strings in the list that contain the substring.

Slicing rule

Python slicing uses [start:stop], not [start:count], so my_list[1][4:5] gets one character starting at index 4.

TL;DR: use my_list[index][start:stop] for substring extraction, and [item for item in my_list if substring in item] for substring searching.