Ask coding questions

← Back to all posts
I need help on some python for loops
MisledWater79 (0)

How would I go about making it so in a for i in str: it will check if i is a ">" then check if the next character is a " ". If its true then continue the for loop, if its not true insert a " ".

Comments
hotnewtop
OldWizard209 (1643)

For this you need to create a list to check.

strng = "Go here> "
list_ = []

for i in strng:
    list_.append(i)

if ">" in list_:
    index_of_symbol = list_.index(">")
    try:
        if list_[index_of_symbol+1] == " ":
            print('There is a " " after the symbol ">".') 
        else:
            print('No " " after symbol ">".')
    except:
        print('No " " after symbol ">".')
Coder100 (18934)

you can go index by index, sort of like this:

string = "> "

for i in range(len(string)):
  if string[i] == ">" and (i + 1 < len(string) and string[i + 1] != ' '):
    # do your inserting

by the way
you should never name your variables str, it's already a function class!

pzrepl (68)

@MisledWater79 try
input = str(input("random string"))
for x in range(len(input)):
if input[x] == ">":
x += 1
if input[x] == " ":
print(">")
continue
else:
print(">")
print(" ")
else:
print(input[x])