Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list without using range in Python (For Loops)?

Let's say I have

myString = "ORANGE"

How can I write a for-each loop that lists each character so it looks like this

1O
2R
3A
4N
5G
6E

I am confused as I don't know how to do it without using range.

like image 815
svlk Avatar asked Feb 02 '26 22:02

svlk


2 Answers

This is quite basic. 5 answers and none of them actually say this I am surprised. Never mind this should do what you asked for:

myString = "ORANGE"
a = 1

for i in myString:

    print(str(a) + i)
    a = a + 1
like image 116
Xantium Avatar answered Feb 05 '26 11:02

Xantium


myString = "ORANGE"
l = [str(i)+j for i,j in enumerate(myString,1)]
' '.join(l)

Output:

'1O 2R 3A 4N 5G 6E'
like image 30
Transhuman Avatar answered Feb 05 '26 13:02

Transhuman