-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,471
  • 17
  • 87
  • 173
Mona Coder
  • 5,994
  • 17
  • 60
  • 118

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
  • 55,057
  • 6
  • 42
  • 65