Accessing String Elements
Use square brackets with an index inside to access a particular string element. An index is not a position of a specific element since indexing in Python starts from 0. For example, watch the example below.
As you can see, the index in Python is always less than position by 1.
1234# Initial string site = "codefinity" # Get the letters 'o' and 'y' print(site[1], site[9])
To get multiple elements at once, you can use slicing. To do it, pass starting and ending indices within square brackets divided by the colon :
sign. Note that the second index will not be included. For example, if you use [1:5]
, you will get all the elements with indices from 1 to 4. You can watch the example below.
As you can see, the last index is greater than the index of the last element by 1.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 2.7
Accessing String Elements
Desliza para mostrar el menú
Use square brackets with an index inside to access a particular string element. An index is not a position of a specific element since indexing in Python starts from 0. For example, watch the example below.
As you can see, the index in Python is always less than position by 1.
1234# Initial string site = "codefinity" # Get the letters 'o' and 'y' print(site[1], site[9])
To get multiple elements at once, you can use slicing. To do it, pass starting and ending indices within square brackets divided by the colon :
sign. Note that the second index will not be included. For example, if you use [1:5]
, you will get all the elements with indices from 1 to 4. You can watch the example below.
As you can see, the last index is greater than the index of the last element by 1.
¡Gracias por tus comentarios!