Python Forum

Full Version: List Syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pls help, what I am doing wrong ? I'm not getting any output, whereas I'm expecting it to return as
String 2

list1 = ["String1", "String2", "String3"]

list1[2]
(May-24-2020, 06:42 PM)Hitso Wrote: [ -> ]Pls help, what I am doing wrong ? I'm not getting any output, whereas I'm expecting it to return as
String 2

list1 = ["String1", "String2", "String3"]

list1[2]

You aren't doing anything with list1[2].

You need to print it print(list1[2]).
As a side note
list1 = ["String1", "String2", "String3"]
list1[2]
will not return string2 but will return string3. index starts at zero
Hi,

If you type this in the shell editor, it will, as expected, output "String3".
As a saved "*.py' file, you will need a print() statement as stated above.
Paul
To get string 2 as output, the code should be
print(list1[1])
Note the index start from zero.Also you don't need print function when using Jupiter notebook.But incase of python editors, you need to use print function to print anything