string_format.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # basic functionality test for {} format string
  2. def test(fmt, *args):
  3. print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')
  4. test("}}{{")
  5. test("{}-{}", 1, [4, 5])
  6. test("{0}-{1}", 1, [4, 5])
  7. test("{1}-{0}", 1, [4, 5])
  8. test("{:x}", 1)
  9. test("{!r}", 2)
  10. test("{:x}", 0x10)
  11. test("{!r}", "foo")
  12. test("{!s}", "foo")
  13. test("{0!r:>10s} {0!s:>10s}", "foo")
  14. test("{:4b}", 10)
  15. test("{:4c}", 48)
  16. test("{:4d}", 123)
  17. test("{:4n}", 123)
  18. test("{:4o}", 123)
  19. test("{:4x}", 123)
  20. test("{:4X}", 123)
  21. test("{:4,d}", 12345678)
  22. test("{:#4b}", 10)
  23. test("{:#4o}", 123)
  24. test("{:#4x}", 123)
  25. test("{:#4X}", 123)
  26. test("{:#4d}", 0)
  27. test("{:#4b}", 0)
  28. test("{:#4o}", 0)
  29. test("{:#4x}", 0)
  30. test("{:#4X}", 0)
  31. test("{:<6s}", "ab")
  32. test("{:>6s}", "ab")
  33. test("{:^6s}", "ab")
  34. test("{:.1s}", "ab")
  35. test("{: <6d}", 123)
  36. test("{: <6d}", -123)
  37. test("{:0<6d}", 123)
  38. test("{:0<6d}", -123)
  39. test("{:@<6d}", 123)
  40. test("{:@<6d}", -123)
  41. test("{:@< 6d}", 123)
  42. test("{:@< 6d}", -123)
  43. test("{:@<+6d}", 123)
  44. test("{:@<+6d}", -123)
  45. test("{:@<-6d}", 123)
  46. test("{:@<-6d}", -123)
  47. test("{:@>6d}", -123)
  48. test("{:@<6d}", -123)
  49. test("{:@=6d}", -123)
  50. test("{:06d}", -123)
  51. test("{:>20}", "foo")
  52. test("{:^20}", "foo")
  53. test("{:<20}", "foo")
  54. # nested format specifiers
  55. print("{:{}}".format(123, '#>10'))
  56. print("{:{}{}{}}".format(123, '#', '>', '10'))
  57. print("{0:{1}{2}}".format(123, '#>', '10'))
  58. print("{text:{align}{width}}".format(text="foo", align="<", width=20))
  59. print("{text:{align}{width}}".format(text="foo", align="^", width=10))
  60. print("{text:{align}{width}}".format(text="foo", align=">", width=30))
  61. print("{foo}/foo".format(foo="bar"))
  62. print("{}".format(123, foo="bar"))
  63. print("{}-{foo}".format(123, foo="bar"))