Python Forum
Unexpected result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unexpected result (/thread-26461.html)



Unexpected result - linton - May-02-2020

Os-Ubuntu 18.04 Python - Ver 3.6 Using Idle .

dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ')
print(dms[0])
Output:
Output:
=============== RESTART: /home/linton/Documents/Python/test.py =============== DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20 5
My expectation from the internet searches I have done is that the output for the index dms[0] should be the element before the first comma - in this case 54 but it only outputs the first digit 5 and not the whole indexed element.


RE: Unexpected result - ndc85430 - May-02-2020

Of course it does - each position in a string is a single character. Where did you find otherwise? You can at least Do something like finding the position of the first comma and then use slicing to get everything from the beginning up to (but excluding) that.


RE: Unexpected result - buran - May-02-2020

your expectation would be true, if dms was list, but it is not. It is a str.
you need to split the str returned by input(). Note that dms[0] will still be str.
dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ').split(',')
print(dms[0])



RE: Unexpected result - anbu23 - May-02-2020

dms holds string. Hence dms[0] gives first char which is expected behaviour.

Use split() as here to convert input to list

>>> dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ')
DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20
>>> dms
'54,20,20'
>>> type(dms)
<class 'str'>
>>> dms[0]
'5'
>>> dmsLst = input('DR_Lat (deg,min,sec,N/S/E/W) = ').split(",")
DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20
>>> type(dmsLst)
<class 'list'>
>>> dmsLst[0]
'54'



RE: Unexpected result - linton - May-02-2020

Thanks buran, anbu23 - got it.