0

I have a string:

s = "abcd 90 96.8% ab" 

I want to extract 90 and 96.8 from the string like this [90, 96.8]

I have tried doing it by regex:

print(re.findall("\d+\.\d+", s))

output: [96.8]

print(re.findall("\d+", s))

output: [90, 96, 8]

Can anyone tell me the solution which extracts int as well as a decimal from string?

0

1 Answer 1

1

You can use a for-loop like this:

s = "abcd 90 96.8% ab"
num = []
for n in s.split(): # For every substring in the string separated by a space
    a = ''
    for l in n: # For every character in the substring
        if l.isdigit() or l == '.': # If the character is a number or a decimal poit
            a += l
    if a:
        num.append(a)
print(num)

Output:

['90', '96.8']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.