0

I know how I can get a substring from a string using index, but I want a substring starting from a substring, and with limited length.

For example. I have the string:

7F4P_gmLYIQottr9LMsJ5_arena_word2_fdsfds_fdsfds_fgdgdfs

I want to get a substring starting from the word arena, but no more than 40 characters. The result should be:

arena_word2_fdsfds_fdsfd

The underscores, "_", are in a way random generated, so I can't just split after the second underscore.

user3541631
  • 3,686
  • 8
  • 48
  • 115
  • Possible duplicate of [Get a string after a specific substring](https://stackoverflow.com/questions/12572362/get-a-string-after-a-specific-substring) – Sayse Oct 18 '19 at 09:50

1 Answers1

2
word = "7F4P_gmLYIQottr9LMsJ5_arena_word2_fdsfds_fdsfds_fgdgdfs"

result = "arena" + word.split("arena")[-1] #append the word "arena" and the characters after the word "arena"

limit_result = result[:40] #limit the result to 40 characters

Final result : "arena_word2_fdsfds_fdsfds_fgdgdfs"