python - What does [1] mean in this code? -


I just started learning python I found this statement:

  output = " Name: abc "log = output.split (" = ") [1]   

what [1] denote? Why is this used?

[1] is listed in the list output.split ( "=") returned by; If that method gives a list of two or more elements, then [1] lists the second element.

In your specific case, this is a IndexError , because there is no = in output . Due to this, the output.split ("=") method returns a list with just a string.

You can try these things at a Python interpreter prompt:

  & gt; & Gt; & Gt; Output = "name: ABC" & gt; & Gt; & Gt; Output.split ('=') ['name: abc']> gt; & Gt; & Gt; Output. Split ('=') [0] 'name: ABC' & gt; & Gt; & Gt; Output. Split ('=') [1] traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; Index error: the list index out of range   

have you broken out on : Instead you have found a more useful result:

  & gt; & Gt; & Gt; Output. Split (':') [1] 'ABC'    

Comments

Popular posts from this blog

python - Writing Greek in matplotlib labels, titles -

c# - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method -

Pygame memory leak with transform.flip -