-1

I'm iterating through a string and need to perform certain actions if the index number of a given character equals a certain value. However I'm not sure of the best way of keeping track of the current index number during the iteration. For example I need code that broadly does the following:

def myfunc(word):
    for n in word:
        if n[index] = 0:
            do this
        elif n[index] = 4:
            do this
        else:
            do this

I just can't seem to find any inbuilt counter or function that allows me to keep track of the current iteration index. I could add a counter in as variable and just +1 after each loop but this seems clunky and I would have thought Python would know the current iteration number of "n" and could report it back?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
TheRobster
  • 43
  • 4
  • 1
    This is part of [the official Python tutorial](https://docs.python.org/3/tutorial/controlflow.html#the-range-function)... – Tomerikoo Jan 21 '22 at 18:35

4 Answers4

3
def myfunc(word):
    
    new_str = ""   #null string
    
    for i, c in enumerate(word):    #creates a series of tuples, each on contains index# and current character
        if i==0 or i==3:
            new_str = new_str + c.upper()   #add the character to new_str but make uppercase
        else:
            new_str = new_str + c   #just add the existing lowercase character
            
    return new_str
TheRobster
  • 43
  • 4
  • Quick question: I take it enumerate doesn't automatically create an object withe the results or store them as it goes along? I.e. I'd have to pass them to an object in order to store them? – TheRobster Jan 21 '22 at 18:50
  • @Robster If you have a new/follow-up question, [post it as a separate question](https://stackoverflow.com/questions/ask). Stack Overflow does not follow the typical forum format where you create a thread to continuously ask questions about your code. – Gino Mempin Jan 22 '22 at 02:42
  • ...although, as the [ask] guide says, please try to research first. Your question about `enumerate` is most likely already answered before (like this [What does enumerate() mean?](https://stackoverflow.com/q/22171558/2745495)) or is already mentioned in the Python docs. – Gino Mempin Jan 22 '22 at 02:46
0

Two ways to do this. Iterate the index i between 0 and the len of the str:

for i in range(len(word)):
  c = word[i]

Or use python's enumerate function to do both at once:

for i, c in enumerate(word):
  ...
micah
  • 7,596
  • 10
  • 49
  • 90
-1

Iterate through index and not the elements:

def myfunc(word):
    for n in range(len(word)):
        if word[n][index] = 0:
            do this
        elif word[n][index] = 4:
            do this
        else:
            do this

In this way, the index that you want is n

C-Gian
  • 62
  • 2
  • 19
  • What does it mean `word[n][index]`? What is `index`? Also `word[n]` would be a letter, why would you need to index it again? – Tomerikoo Jan 21 '22 at 18:39
-1
def myfunc(word):
    for index in range(len(word)):
       if (index==0):
           do this
       elif (index==4):
           do this
       else:
           do this
Faraz
  • 67
  • 7
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 25 '22 at 09:15