user3642744 user3642744 - 2 months ago 4x
Python Question

how to remove first x characters from a string in a list

So I have a list and a couple of string in it. I just want to remove the first 7 characters from each of the strings. How do I do that?

I've tried:

lst = ["1234567something", "1234567smthelse"]

for i in lst:

i [7:]

print lst

But I get the same list from the beginning...

3kt 3kt

When doing i [7:] you are not actually editing the element of the list, you are just computing a new string, without the first 7 characters, and not doing anything with it.

You can do this instead :

>>> lst = [e[7:] for e in lst]
>>> lst
['something', 'smthelse']

This will loop on the elements of your array, and remove the characters from the beginning, as expected.