Home > OS >  how to get a variable within a string to a python variable
how to get a variable within a string to a python variable

Time:03-12

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?

CodePudding user response:

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 '='.

  • Related