Why I am getting IndexError: tuple index out of range when converting a float value to a string?

Terry Reedy tjreedy at udel.edu
Tue Jun 11 16:11:55 EDT 2019


On 6/11/2019 1:04 PM, Rob Gaddi wrote:
> On 6/11/19 9:52 AM, madhavanbomidi at gmail.com wrote:
>> I wrote the following lines of code:
>>
>> xm = np.nanmean(x[indx],axis=None,dtype=float)
>> xsd = np.nanstd(x[indx],axis=None,dtype=float)
>>
>> type(xm)    # np.float64
>> type(xsd    # np.float64
>>
>> print(xm)      # 0.5414720812182742
>> print(xsd)     # 0.15748041033663002
>>
>> print(str("{6.5f}".format(xm)))
>>
>> I get the following error:
>>
>> IndexError: tuple index out of range
>>
>> Can someone suggest me why I am getting this error and how to overcome 
>> this?

Show us the full traceback after expanding and reducing the example to 
the minimum needed to exhibit the problem.

> print(str("{:6.5f}".format(xm)))
> 
> You want to apply the format 6.5f to the autoindexed element.

Which works fine as it is.

 >>> xm = 3.0
 >>> print(str("{:6.5f}".format(xm)))
3.00000

> You could also explicitly index the 1st element (#0) as {0:6.5f}.

He could, but it makes no difference for the print statement above.

Applying str to an str is redundant, as it applying it to a print argument.

 >>> print("{0:6.5f}".format(xm))
3.00000


-- 
Terry Jan Reedy





More information about the Python-list mailing list