mmmmmmmmmmm
mmmmmmmmmmm

Reputation: 11

How to cut strings inside a list in python

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

Answers (2)

knl
knl

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

mackorone
mackorone

Reputation: 1066

You can use string slicing for this:

x = "abcde"
print(x[1:4])  # prints elements [1, 4), i.e., "bcd"

Upvotes: 0

Related Questions