-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
  • 19,256
  • 7
  • 31
  • 50
John
  • 309
  • 4
  • 14

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
  • 19,256
  • 7
  • 31
  • 50