Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Syntax
#1
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]
Reply
#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]).
Reply
#3
As a side note
list1 = ["String1", "String2", "String3"]
list1[2]
will not return string2 but will return string3. index starts at zero
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
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
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is the correct syntax for list items that need to contain a quotation mark, etc? KaisoArt 7 3,431 Sep-14-2019, 05:26 AM
Last Post: buran
  syntax error on list.sort() jjordan33 3 3,146 Jul-10-2019, 04:57 PM
Last Post: jefsummers
  list syntax blazersnake 6 3,831 Feb-28-2018, 07:48 PM
Last Post: buran
  Help with list Syntax. jarrod0987 2 2,591 Jan-13-2018, 01:11 PM
Last Post: jarrod0987

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020