Mohammed Al-Khayyat
Mohammed Al-Khayyat

Reputation: 1

Loop in numbers as a whole and string as singular

I want a function that loops through the text and print out the letters alone and the numbers alone.

I have

function("A10")

The result should be:

A
10

For:

    function("A1B3")

The result should be :

A
1
B
3

For C12H30

The result should be :

C
12
H
30

Thanks

Upvotes: 0

Views: 94

Answers (3)

K. M. Rajib Faysal
K. M. Rajib Faysal

Reputation: 46

given_string = "A10"

for i in string:
    if i.isalpha():
        print(i)
    else:
        print(i, end='')

Upvotes: 3

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

You could index the first value:

>>> name[0]
'A'

You could slice the rest:

>>> name[1:]
'10'

In code:

print(name[0])
print(name[1:])

Output:

A
10

Upvotes: 1

MoShoukri
MoShoukri

Reputation: 1

when you use "for loop" it loop letters in the word, every letter in a single line

A
1
0

Upvotes: -2

Related Questions