-3

I have an array of 10 elements and I can each of the elements print on a new line formatted 6.2f with

print(f'{myArray:6.2f}', sep="\n")

But I would like to create string containing what needs to be printed, add a few things and print that string, like:

 text = 'something ' + f'{myArray:6.2f}' + ' rest' print(text) 

How can I make that each element of myArray will be on a new line and formatted in text?

5
  • There has to be an existing Q&A showing how to combine format + generator expression + str.join Commented Oct 21, 2024 at 16:38
  • Yes, I know that there 'has to be' an existing Q&A but I didn't find it and unfortumally you didn't show me. I don/t aks here because I'm too lazy to search myself. Commented Oct 21, 2024 at 19:53
  • Can you show what exactly your input and expected output is? In some comments you mentioned nested lists but the question doesn't say that. Commented Oct 22, 2024 at 6:53
  • Thanks for reply, Expected outcome: "results are:\n 1.23 -2.34 12.87 \n -3.45 8.98 14.38 \n 2.97 1.23 75.32\n enter new data" Input (after reading an answer): text = ''.join([f'\n{item}' for item in myArray]) text = "results are:" + text + "\nenter new data") Indeed I have a nested list but didn't mention it as I thought/hoped I could figure out myself handling the nested list once I knew the first step Commented Oct 22, 2024 at 11:40
  • Thanks for ALL the replies, they all helped me towards the solution including expansion of the list requiering different formats to use. text = "results:\n" + '\n'.join(''.join(f"{x[0]:6.2f} {x[1]:6.2f} {x[2]:6.4f} {x[3]:3.1f}".format(*x)) for x in myArray) + "\n Enter new data" Only thing left is that I would like to put the different formats into a list and use that for each nested list of myArray. Commented Oct 22, 2024 at 13:30

3 Answers 3

2

You can try this:

for item in myArray: print (f'something {item:6.2f} rest', sep="\n")) 
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

arr=map(float, range(1,11)) print('\n'.join([f'something {item:6.2f} rest' for item in arr])) 

Prints:

something 1.00 rest something 2.00 rest something 3.00 rest something 4.00 rest something 5.00 rest something 6.00 rest something 7.00 rest something 8.00 rest something 9.00 rest something 10.00 rest 

3 Comments

Thank you. but what if myArray consists of a list of floats like myArray[0] = [1.23456, 2.4680, 13.579] etc instead of single float? How can I print the contents of myArray[0] using 6.2f ?
So your array is nested? Something like [[1.23,3.45,6.78],[9.10,10.23,11.22],...]?
Indeed, currently the nested lists all contain 3 floats, but it might be I will add some ints and/or texts to each of the nested lists. (All nested lists will have the same layout)
1

Make a template and apply it elementwise. Format & f-string notation do not require an explicit cast to string which make them preferable wrt an explicit string join, str.join.

Notice that for the f-string notation {} are escaped by nesting them {{}}. Then, to use format, you need a further nested pair.

You can pass your own element separator to the print to customize, with no extra manipulation, the final output.

myArray = [[1.23456, 2.4680, 13.579], [1, 2, 3]] sf = 6 # significant figures dp = 2 # decimal places apply_float_pattern = f"{{:{sf}.{dp}f}}".format el_SEP = ', ' line_SEP = '\n' # method 1 for a in myArray: print(*map(apply_float_pattern, a), sep=el_SEP) 

Ouptut

 1.23, 2.47, 13.58 1.00, 2.00, 3.00 

or by constructing a single string representing the whole output

# method 2 output = line_SEP.join(el_SEP.join(map(apply_float_pattern, a)) for a in myArray) print(output) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.