Search code examples
pythonlisterror-handlinggotopython-turtle

ValueError: could not convert string to float: '[108.62257164103005]'


I am trying to convert a list to a float so have first converted it into a string but when I try to convert the string into a float it doesn't work and gives me the error: ValueError: could not convert string to float: '[108.62257164103005]' Sorry if I am being a noob, thanks in advance.


Solution

  • I am not sure what the brackets are for, but you can convert string to float like this:

    str1 = '108.62257164103005'
    str2 = float(str1)
    
    print(type(str2))
    print(str2)
    

    Gives you:

    <class 'float'>
    108.62257164103005
    

    Or if you want to go from list to float, just iterate over it

    str0 = ['108.62257164103005']
    for i in str0:
        print(float(i))