what is “{:<20}” format_string meaning in python?
本问题已经有最佳答案,请猛点这里访问。
格式字符串中的:<20:>4是什么意思?
初学者
1 2 3 4 5 6 7 8 9 10 11 12 | popularity = [["Language", 2017, 2012, 2007, 2002, 1997, 1992, 1987], ["Java", 1, 2, 1, 1, 15, 0, 0], ["C", 2, 1, 2, 2, 1, 1, 1], ["C++", 3, 3, 3, 3, 2, 2, 5], ["C#", 4, 4, 7, 13, 0, 0, 0], ["Python", 5, 7, 6, 11, 27, 0, 0], ["Visual Basic .NET", 6, 17, 0, 0, 0, 0, 0], ["PHP", 7, 6, 4, 5, 0, 0, 0], ["JavaScript", 8, 9, 8, 7, 23, 0, 0], ["Perl", 9, 8, 5, 4, 4, 10, 0]] format_string ="{:<20} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4}" |
DOC在这里:P></
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
'<': Forces the field to be left-aligned within the available space (this is the default for most objects).
'>': Forces the field to be right-aligned within the available space (this is the default for numbers).
我知道
这样看:P></
1 | for l in popularity: print(format_string.format(*l)) |
输出:P></
1 2 3 4 5 6 7 8 9 10 | Language 2017 2012 2007 2002 1997 1992 1987 Java 1 2 1 1 15 0 0 C 2 1 2 2 1 1 1 C++ 3 3 3 3 2 2 5 C# 4 4 7 13 0 0 0 Python 5 7 6 11 27 0 0 Visual Basic .NET 6 17 0 0 0 0 0 PHP 7 6 4 5 0 0 0 JavaScript 8 9 8 7 23 0 0 Perl 9 8 5 4 4 10 0 |
顺便,Python在2017年前和1列,在2018年前stays在线。P></