[SOLVED] how to get a variable within a string to a python variable


If I have a few strings:

string1 = 'text here pri=1 more text urgent=True'
string2 = 'more text here pri=5 urgent=False'
string3 = 'another text pri=3 urgent=False'
string4 = 'more text pri=10 urgent=True'

how can I grab the value of "pri" and "urgent" from the string to a variable?

I came up with a few solutions like for pri just locating it using str.find and then jumping 4 variables ahead, etc….

but there must be a more efficient way to read it, how can I do so?


you can just use the .split() function to get all the different words. It would return a list. For string 1:
['text', 'here', 'pri=1', 'more', 'text', 'urgent=True']
so now you can loop through the list and search for ‘=’.

Answered By – TheCoder1001

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *