+ 1

Error

course = "python programing" print(course{1}) Hi im trying to run this it should give me the 2nd latter of python ..y... Instead it give me syntax error and it says Maybe you forgot a comma. And when i add one it give Another erroe🥹🥲

11th May 2024, 6:17 AM
Hosein
Hosein - avatar
4 Antworten
+ 7
Hosein , here some more details for better understanding: in this case we are using a string "python programing" stored in a variable named `course`, which is a sequence of characters. sequences are also called iterables, because we can iterate over the elements. each element can be accessed by a number that is called `index`. index counting starts at the left side of the iterable with 0, the next index is 1 and so on. to access an element of the variable, we can use the index number in combination with the name of the variable. index has to be put in square brackts like [0] for the first element. this index notation has to added to the variable name like: course[0] # this will return the letter `p` course[1] # this will return the letter `y` we can store the result of the operations described above, or we can print the result directly using: print(course[1]) # result is `y`
11th May 2024, 2:43 PM
Lothar
Lothar - avatar
+ 7
Hosein , Curly brace({ }) represents dictionary... You are doing array operation...for that you have to use square brackets ([ ])... Check here, https://sololearn.com/compiler-playground/c6UfS9mAY56E/?ref=app
11th May 2024, 7:31 AM
Riya
Riya - avatar
+ 4
Hosein , replace the brackets. Instead of "{}" use "[]".
11th May 2024, 6:27 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
You Used "{}" instead of "[]" Here Is The Corrected Code - course = "python programming" print(course[1])
12th May 2024, 2:46 AM
JaiprakashJH
JaiprakashJH - avatar