-1

How can I remove the third _ and all strings before and also the 4th _ and all string after that using Python Replace method

st = "the_gis_osm_natural_a_free_1.shp"
halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

1 Answers1

3

You could split the string by _ and then join the result:

start, end = 3, 4
filename = "the_gis_osm_natural_a_free_1.shp"
print('_'.join(filename.split("_")[start:end]))

Output

natural
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76