Get exact value in for loop

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mrpaolosarino

I want to get the exact index for each loop. Let say:

for i in "Joe":

I want to print J o e

if you try to print the for loop above, you will get:

J
o
e

This is just a sample problem but anyone who knows??

:bust_in_silhouette: Reply From: exuin

I’m not 100% sure what you’re asking, but i is the exact character in the word Joe. It’s just that the print function adds a new line after it. If you want add a space between each letter you’ll need to make a new string and add a space after you add each letter to the new string.

I think what I meant is in

for i in "Joe": 

what I want to do is

print(i[0], " " ,i[1] , " " , i[2])

I know that i[0] doesn’t exist and I’m asking if there any such thing (alternative) as “i[0]” where you get the loop’s index??

Mrpaolosarino | 2021-04-07 00:40

Okay so do this

var str = "Joe"
var result = ""
for i in len(str):
    result += str[i] + " "

In this for loop i is the index.

exuin | 2021-04-07 00:46

Bro that’s really a great help!!

Mrpaolosarino | 2021-04-07 00:51