I'm a Python beginner, so how do I print out letters by numbers after receiving letters and numbers together?

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

I've been thinking about it for 4 hours, but I don't have a proper answer

If you enter A5B100, you output 5 A and 100 B.

A function that outputs 80 A and 10 B when A80B10 is entered

I want to make it, but there's a lot to consider whether it's a number length or a character to make it a number. What should I do?

python

2022-09-20 15:03

2 Answers

Please refer to it.

a = 'A5B100'
b = int(a.split('B')[0].split('A')[1])
c = int(a.split('B')[1])


2022-09-20 15:03

I think he probably wanted something like this. There are string functions such as isalpha and isnumerical.

I just roughly planned it out. Please refer to it.

>>> def get_str_count_pair(s: str):
    s1 = ''
    for i, c in enumerate(s):
        if c.isalpha():
            s1 += c
        else:
            s = s[i:]
            break
    print(s1, s)
    n1 = ''
    for i, c in enumerate(s):
        if c.isnumeric():
            n1 += c
        else:
            s = s[i:]
            break
    s2 = ''
    for i, c in enumerate(s):
        if c.isalpha():
            s2 += c
        else:
            s = s[i:]
            break
    print(s2, s)
    n2 = ''
    for i, c in enumerate(s):
        if c.isnumeric():
            n2 += c
        else:
            s = s[i:]
            break
    return s1 *(int(n1)) + s2*(int(n2))

>>> get_str_count_pair("A5B10")
A 5B10
B 10
'AAAAABBBBBBBBBB'
>>> get_str_count_pair("AX3B7")
AX 3B7
B 7
'AXAXAXBBBBBBB'


2022-09-20 15:03

If you have any answers or tips


© 2022 pinfo. All rights reserved.