-1

If i had a list as an example:

a = ['Hello_1.txt', 'Hello_2.txt']

In Python is it possible to somehow remove the first 5 characters ('Hello') and the last 3 characters ('txt') from each of the items in the list ?

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
John
  • 321
  • 5
  • 16

1 Answers1

1

You could use a list-comprehension and string slicing:

[s[5:-3] for s in a]

which gives what you describe (not sure this is the neatest output though!)

['_1.', '_2.']
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54