0

Assuming I have this string:

TEST = 'GAMES HELLO VAUXHALL RUNS=15 TESTED=3'

if the text 'RUNS=' exists in TEST, I want to extract the value 15 into a variable, nothing else from that string.

2 Answers 2

1

You can use re module:

import re

s = "GAMES HELLO VAUXHALL RUNS=15 TESTED=3"


m = re.search(r"RUNS=(\d+)", s)
if m:
    print("RUN=... found! The value is", m.group(1))

Prints:

RUN=... found! The value is 15
Sign up to request clarification or add additional context in comments.

Comments

0

Just split the line and check if one of the items starts with your desired string:

TEST = 'GAMES HELLO VAUXHALL RUNS=15 TESTED=3'

def extract(s: str):
    for i in s.split():
        if i.startswith("RUNS="):
            return int(i.split("=")[1])

print(extract(TEST))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.