How to concatenate strings with iteration in a loop?

Frank Millman frank at chagford.com
Tue May 21 04:22:56 EDT 2019


On 2019-05-21 9:42 AM, Madhavan Bomidi wrote:
> Hi,
> 
> I need to create an array as below:
> 
> tempStr = year+','+mon+','+day+','+str("{:6.4f}".format(UTCHrs[k]))+','+ \
> str("{:9.7f}".format(AExt[k,0]))+','+str({:9.7f}".format(AExt[k,1]))+','+ \
> str("{:9.7f}".format(AExt[k,2]))+','+str("{:9.7f}".format(AExt[k,3]))+','+ \
> str("{:9.7f}".format(AExt[k,4]))+','+str("{:9.7f}".format(AExt[k,5]))+','+ \
> str("{:9.7f}".format(AExt[k,6]))+','+str("{:9.7f}".format(AExt[k,7]))+','+ \
> str("{:9.7f}".format(AExt[k,8]))+','+str("{:9.7f}".format(AExt[k,9]))
> 
> 
> k is a row index
> 
> Can some one suggest me how I can iterate the column index along with row index to concatenate the string as per the above format?
> 
> Thanks in advance
> 

The following (untested) assumes that you are using a reasonably 
up-to-date Python that has the 'f' format operator.

tempStr = f'{year},{mon},{day},{UTCHrs[k]:6.4f}'
for col in range(10):
     tempStr += f',{AExt[k, col]:9.7f}'

HTH

Frank Millman






More information about the Python-list mailing list