#value print("{0}, {1}, {2}".format("1", "2", "3")) #value print("I love {name}".format(name="dog")) #integer print("integer:{:d}".format(10)) print("integer:{:d}".format(12345)) #Expressing a percentage print("{:.1%}".format(15)) print("{:.2%}".format(155)) print("{:.3%}".format(1555)) #float print("float:{:+f}".format(3.14)) print("float:{:+f}".format(3.1415)) print("float:{:+f}".format(3.121592)) #double print("double:{:+e}".format(3.14)) print("double:{:+e}".format(3.1415)) print("double:{:+e}".format(3.141592)) print("double:{:+E}".format(3.14)) print("double:{:+E}".format(3.1415)) print("double:{:+E}".format(3.141592)) #HEX print("HEX:{:x}".format(10)) print("HEX:{:x}".format(0x10)) #OCT print("OCT:{:o}".format(10)) print("OCT:{:o}".format(0x10)) #BIN print("BIN:{:b}".format(10)) print("BIN:{:b}".format(0x10)) #Using the comma as a thousands separator: print("{:,}".format(123456789)) #Aligning the text and specifying a width print("{:<20}".format("test test test")) print("{:>20}".format("test test test")) print("{:^20}".format("test test test")) print("{:*^20}".format("test test test")) #date import datetime d = datetime.datetime(2014, 9, 18, 12, 00, 00) print("{:%Y-%m-%d %H:%M:%S}".format(d)) d = datetime.datetime(2014, 9, 18) print("{:%Y-%m-%d}".format(d))
參考資料:
http://docs.python.org/3/library/string.html#format-string-syntax