Python Forum

Full Version: Unexpected result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Os-Ubuntu 18.04 Python - Ver 3.6 Using Idle .

dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ')
=============== RESTART: /home/linton/Documents/Python/ =============== 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.
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.
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(',')
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
>>> type(dms)
<class 'str'>
>>> dms[0]
>>> 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]
Thanks buran, anbu23 - got it.