0 votes

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??

Godot version 3.2.3
in Engine by (378 points)

1 Answer

0 votes

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.

by (7,190 points)

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??

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.

Bro that's really a great help!!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.