简体   繁体   中英

How to use string formatting within a list?

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?

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)]

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-string s 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-string s:

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

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.

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.

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM