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?
-
1You have to call format on each string there. Or just use f-strings for an easier time. Also see pyformat.infokichik– kichik2021年11月03日 03:01:13 +00:00Commented Nov 3, 2021 at 3:01
-
I hope the problem is a bit more obvious after I edited to improve the code stylewjandrea– wjandrea2021年11月03日 03:09:09 +00:00Commented Nov 3, 2021 at 3:09
-
BTW, welcome to Stack Overflow! Check out the tour, and How to Ask if you want tips.wjandrea– wjandrea2021年11月03日 03:13:54 +00:00Commented 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 oneantwanne456– antwanne4562021年11月03日 03:21:44 +00:00Commented Nov 3, 2021 at 3:21
-
1What is the expected output?PCM– PCM2021年11月03日 03:23:00 +00:00Commented Nov 3, 2021 at 3:23
6 Answers 6
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)]
Comments
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
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
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
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
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)