0

Is it possible to use the string formatting method within a list?

For example

list1 = ["{0:^8}", "{1:^8}", "{2:^8}".format(7, 8, 9)]

But whenever I try to run it it gives the output as only having changed the last one.

['{0:^8}', '{1:^8}', '   9    ']

How to format the complete list?

6
  • 1
    You have to call format on each string there. Or just use f-strings for an easier time. Also see pyformat.info Commented Nov 3, 2021 at 3:01
  • I hope the problem is a bit more obvious after I edited to improve the code style Commented Nov 3, 2021 at 3:09
  • BTW, welcome to Stack Overflow! Check out the tour, and How to Ask if you want tips. Commented Nov 3, 2021 at 3:13
  • Hi! I actually wanted to treat each element in the list as separate and not treat them with a single inverted commas as one Commented Nov 3, 2021 at 3:21
  • 1
    What is the expected output? Commented Nov 3, 2021 at 3:23

6 Answers 6

3

You're only formatting the last string. You just need to loop over the numbers, and since the format spec is the same for all of them, you can reuse it*.

>>> ['{:^8}'.format(x) for x in (7, 8, 9)]
['   7    ', '   8    ', '   9    ']

* As opposed to a different spec for each one, for which you could use zip, like

[spec.format(x) for spec, x in zip(specs, numbers)]
Sign up to request clarification or add additional context in comments.

Comments

1

You're only calling str.format on one string in the list. Your code is working properly, not in the way you want, but in the way you coded it.

So there's really only 2 clear ways to do this imo.

Your values are 7, 8, 9 let's store them into a variable. Then we can use map on them to data which we imply are the formats for each string:

>>> vals = 7, 8, 9
>>> data = ["{:^8}", "{:^8}", "{:^8}"]
>>> list(map(str.format, data, vals))
['   7    ', '   8    ', '   9    ']

Or using f-strings without implying data first, as all the formatting is the same for each value:

>>> vals = 7, 8, 9
>>> [f'{v:^8}' for v in vals]

For an alternative I guess you could use str.format in a list comprehension as well but this isn't as clean or fancy as f-strings:

>>> vals = 7, 8, 9
>>> ['{:^8}'.format(v) for v in vals]

Comments

0

You could use f-strings with list comprehension to get the output -

output = [f'{i:^8}' for i in [7, 8, 9]]
print(output)

In your code, the .format is for the last element, so only that will be formatted.

However, here, all the items are formatted, then added to the list.

Comments

0

The format function works on only one string. So if you have multiple strings then you need to call the format method on each of them either through a loop or individually on each one of them. "{} {} {}".format(7,8,9) works as it is one complete string, but "","","{}".format(7,8,9) applies the last value to that place holder in the last string as it is only called on it.

Comments

-1

This is a simple fix. You are expecting to format all elements individually which would require separate format calls. In reality you want to separate the string elements as their own elements in the list using commas for each item enclosed in quotations. This way formatting can be done on each item.

# You need to adjust your string quotations like this
list1=["{0:^8},{1:^8},{2:^8}".format(7,8,9)]

# Check the results of the adjustment
for item in list1:
    print(item)

1 Comment

Are you sure that's what OP wants? It really doesn't look like it to me. It seems strange to create a list with one element. Also, could you clarify "separate the string elements as their own elements in the list"? cause that sounds like the opposite of what you're doing.
-1
list1=["{0:^8}","{1:^8}","{2:^8}"]    #Insert special characters into a string
str1='|'.join(list1)
list2=str1.format(7,8,9).split('|')
print(list2)

3 Comments

Why would you make a string if you are not gonna use it?
Could you please edit and add an explanation about why this is a good solution? I don't see why you would use this.
yeah,it's look carry coals to Newcastle。

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.