Home > Enterprise >  How to check the next symbol(character) during for loop
How to check the next symbol(character) during for loop

Time:05-01

with open("Base_Of_Cars", 'r') as File:
    for symbol in File[::-1]:
        if symbol == "№"

In this code I should return the symbol which is just after "№", furthermore as you can see the for loop is acting reversingly because of [::-1], so I need to find it from the end.

CodePudding user response:

You can set a variable prev_char = symbol to keep a track of the previous character of symbol. Then check if symbol is equal to :

with open("Base_Of_Cars", 'r') as File:
    for symbol in File[::-1]:
        if symbol == "№":
            # Do something
            print(prev_char)
        prev_char = symbol
  • Related