Search code examples
pythonstringsplitjinja2

How to get the last characters after the third "_" in Jinja


Need help to get the string after the third "_"

Example

r_a_B_TEXAS
r_asda_Bads_DALLAS
r_asda_Bads_DISNEY_LAND
rdd_asda_Bads_Florida_MIAMI_NICE

I need this output:

TEXAS
DALLAS
DISNEY_LAND
Florida_MIAMI_NICE

With split function:

hostname.split('_')[3]

I only get the third value but for DISNEY_LAND I only get DISNEY and wanna have everything DISNEY_LAND


Solution

  • You should be able to specify the max splits that you want to perform to accomplish this:

    >>> string = "r_asda_Bads_DISNEY_LAND"
    >>> string.split("_", 3)[3]
    'DISNEY_LAND'
    

    See https://docs.python.org/3/library/stdtypes.html#str.split