Iterating a string inside the for

Asked

Viewed 109 times

0

As for can pick up the string backwards using Len with -1 -1 -1?

nome = 'felipe'
for c in range(len(nome)-1, -1, -1):
    print(nome[c], c)

inserir a descrição da imagem aqui

  • 4

    Welcome to [en.so]! You have posted an image of the code and/or error message. Although it sounds like a good idea, it’s not! One of the reasons is that if someone wants to answer the question, they can’t copy the code and change something inside. Click on the [Edit] link and put the code/error as text. See more about this in these links - Manual on how NOT to ask questions, Post Error Message as Picture

2 answers

2


Just see the documentation of range. But basically, a range has 3 values:

  • initial value
  • final value
  • footstep

Where the initial value is included and the end is not (for example, for a range with initial value 1, final value 5 and step 1, the values it includes would be 1, 2, 3 and 4).

And to create a range, you can pass 1, 2 or 3 parameters. Ex:

  • range(7): with only one parameter, this becomes the final value. The initial value is set to zero and the step will be 1. Therefore, this range covers 0, 1, 2, 3, 4, 5 and 6.
  • range(2, 7): with two parameters, these become the initial and final value. The step is set to 1. Therefore, this range covers 2, 3, 4, 5 and 6.
  • range(1, 7, 2): with three parameters, these become the initial, final and step value. Therefore, this range covers the values 1, 3 and 5 (the step defines that the values jump from 2 to 2, and remember that the final value - the 7 - is not included).

Only that one range may also be in descending order, but for this the step must be negative. For example, range(5, -1, -1) has the initial value 5, final value -1 and step -1, so it includes the values 5, 4, 3, 2, 1 and 0, in this order.

In the case of your code, the initial value is len(nome) - 1, that is, the size of the string nome (len(nome)) minus 1 (therefore, 5). That is, first you will get the character of position 5 (which is the last, since the first position is zero), after position 4, and so on.


Just for the record, it would also be possible to do the same using enumerate (to obtain the items and their respective indices) together with reversed to reverse the order:

nome = 'felipe'
for indice, caractere in reversed(list(enumerate(nome))):
    print(caractere, indice)

Finally, be sure to watch too other ways to invert a string.

  • So that means Len(name) - 1 will be 5 ? and goes to 0 because -1 is not included, then it will return a house with -1 ? I also noticed that Len(name) counts as 6 so that’s why you put the -1 to stay 5 that actually this 5 goes from 0 to 4?

  • 1

    @Felipesoares nome is a string with 6 characters, so len(nome) is 6, and therefore len(nome) - 1 is 5. Then the created range is range(5, -1, -1), that is, it goes from 5 to -1 (where -1 is not included, so it stops at zero), and the step is -1, that is, it runs through the values 5, 4, 3, 2, 1 and 0, in this order

  • Man! I think I understand, thank you very much! you helped me a lot!

2

The function "Len()" is a builtin of python it returns the string size ( straight to the subject comes internally in the language)

>>> len("felipe")
6

To manipulate strings you can use the Slice used to slice strings, lists, tuples..

string = "python" 

string[start,stop,step]

#start : inicia em uma dada posição
#stop : para em uma posição
#step : pule

###############

string[1:4]

p | y | t | h | o | n
0 | 1 | 2 | 3 | 4 | 5

string[1:4]
'yth'

#iniciei no indice 1 até a posição 4
#então cortei a string de acordo com a posição

string[0:6:1] 
'python'

string[0:6:2] #iniciei na posição 0 até 6 e foi indicado que pule 2 em 2 posição
'pto'


para inverter basta seguir o inverso da posição:
string[::1]# nao foi adicionado o inicio nem onde deve parar mas o pula foi indicado 1 em 1
'python'
string[::-1]# negativo inverta
'nohtyp'



for indice in nome[::-1]:
  print(indice)
 
e
p
i
l
e
f

for indice in nome[::1]: 
  print(indice)
f
e
l
i
p
e
  • Thanks brother!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.