2016-03-15 13:07:41 +00:00
|
|
|
# basic functionality test for {} format string
|
2014-04-01 15:10:02 +00:00
|
|
|
|
2014-03-31 04:06:50 +00:00
|
|
|
def test(fmt, *args):
|
|
|
|
print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')
|
|
|
|
|
2015-03-14 21:20:58 +00:00
|
|
|
test("}}{{")
|
2014-03-31 04:06:50 +00:00
|
|
|
test("{}-{}", 1, [4, 5])
|
|
|
|
test("{0}-{1}", 1, [4, 5])
|
|
|
|
test("{1}-{0}", 1, [4, 5])
|
|
|
|
test("{:x}", 1)
|
|
|
|
test("{!r}", 2)
|
|
|
|
test("{:x}", 0x10)
|
|
|
|
test("{!r}", "foo")
|
|
|
|
test("{!s}", "foo")
|
2014-04-01 15:10:02 +00:00
|
|
|
test("{0!r:>10s} {0!s:>10s}", "foo")
|
|
|
|
|
|
|
|
test("{:4b}", 10)
|
|
|
|
test("{:4c}", 48)
|
|
|
|
test("{:4d}", 123)
|
|
|
|
test("{:4n}", 123)
|
|
|
|
test("{:4o}", 123)
|
|
|
|
test("{:4x}", 123)
|
|
|
|
test("{:4X}", 123)
|
|
|
|
|
2014-04-07 18:19:51 +00:00
|
|
|
test("{:4,d}", 12345678)
|
|
|
|
|
2014-04-01 15:10:02 +00:00
|
|
|
test("{:#4b}", 10)
|
|
|
|
test("{:#4o}", 123)
|
|
|
|
test("{:#4x}", 123)
|
|
|
|
test("{:#4X}", 123)
|
|
|
|
|
2014-04-07 18:19:51 +00:00
|
|
|
test("{:#4d}", 0)
|
|
|
|
test("{:#4b}", 0)
|
|
|
|
test("{:#4o}", 0)
|
|
|
|
test("{:#4x}", 0)
|
|
|
|
test("{:#4X}", 0)
|
|
|
|
|
2014-04-01 15:10:02 +00:00
|
|
|
test("{:<6s}", "ab")
|
|
|
|
test("{:>6s}", "ab")
|
|
|
|
test("{:^6s}", "ab")
|
2015-03-14 21:20:58 +00:00
|
|
|
test("{:.1s}", "ab")
|
2014-04-01 15:10:02 +00:00
|
|
|
|
|
|
|
test("{: <6d}", 123)
|
|
|
|
test("{: <6d}", -123)
|
|
|
|
test("{:0<6d}", 123)
|
|
|
|
test("{:0<6d}", -123)
|
|
|
|
test("{:@<6d}", 123)
|
|
|
|
test("{:@<6d}", -123)
|
|
|
|
|
|
|
|
test("{:@< 6d}", 123)
|
|
|
|
test("{:@< 6d}", -123)
|
|
|
|
test("{:@<+6d}", 123)
|
|
|
|
test("{:@<+6d}", -123)
|
|
|
|
test("{:@<-6d}", 123)
|
|
|
|
test("{:@<-6d}", -123)
|
|
|
|
|
|
|
|
test("{:@>6d}", -123)
|
|
|
|
test("{:@<6d}", -123)
|
|
|
|
test("{:@=6d}", -123)
|
|
|
|
test("{:06d}", -123)
|
|
|
|
|
2016-01-04 13:13:39 +00:00
|
|
|
test("{:>20}", "foo")
|
|
|
|
test("{:^20}", "foo")
|
|
|
|
test("{:<20}", "foo")
|
|
|
|
|
2020-01-22 19:53:54 +00:00
|
|
|
# formatting bool as int
|
|
|
|
test('{:d}', False)
|
|
|
|
test('{:20}', False)
|
|
|
|
test('{:d}', True)
|
|
|
|
test('{:20}', True)
|
|
|
|
|
2016-01-29 09:09:10 +00:00
|
|
|
# nested format specifiers
|
|
|
|
print("{:{}}".format(123, '#>10'))
|
|
|
|
print("{:{}{}{}}".format(123, '#', '>', '10'))
|
|
|
|
print("{0:{1}{2}}".format(123, '#>', '10'))
|
|
|
|
print("{text:{align}{width}}".format(text="foo", align="<", width=20))
|
|
|
|
print("{text:{align}{width}}".format(text="foo", align="^", width=10))
|
|
|
|
print("{text:{align}{width}}".format(text="foo", align=">", width=30))
|
|
|
|
|
2015-01-03 22:14:13 +00:00
|
|
|
print("{foo}/foo".format(foo="bar"))
|
|
|
|
print("{}".format(123, foo="bar"))
|
|
|
|
print("{}-{foo}".format(123, foo="bar"))
|