Reputation: 11
For example the list will contain
list1 = ["mathematics", "sciences", "historical"]
and i only need the middle parts of the string
output_list = ["thema", "ience", "stori"]
Upvotes: 0
Views: 84
Reputation: 1051
Python list comprehension. Please read it up, it's useful.
Also, python slicing and indexing. You can read more here.
For the problem you post:
output_list = [word[2:7] for word in list1]
print(output_list)
Upvotes: 3
Reputation: 1066
You can use string slicing for this:
x = "abcde"
print(x[1:4]) # prints elements [1, 4), i.e., "bcd"
Upvotes: 0