3

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.

1
  • "But as a list so everything goes down vertically and not across." => ??? Sorry but I don't understand what you mean... Commented Oct 6, 2017 at 15:27

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Output:

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

Comments

1
for index, character in enumerate('ORANGE'):
    print('{}{}'.format(index + 1, character))

Python docs on enumerate

Comments

1

Simple way to do this:

myString = "ORANGE"
for i in range(0,len(myString)):
    print (str(i+1)+""+myString[i])

Without using range:

myString = "ORANGE"
j=1
for i in myString:
    print (str(j)+""+i)
    j+=1

Comments

0

try this

myString = "ORANGE"
for id, val in enumerate(myString, 1):
    print("".join(str(id) + val))

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.