Python¸ñʽ»¯
²Î¿¼:¡¶Python3³ÌÐò¿ª·¢Ö¸ÄÏ µÚ¶þ°æ¡·
%¸ñʽ»¯
»ù±¾Ê¹ÓÃ
s = "name: %s, age: %d, weight: %fkg" % ("Tom", 20, 60.5)
print(s)
s = "name: %(name)s, age: %(age)d, weight: %(weight)fkg" % {
"name": "Tom", "age": 20, "weight": 60.5
}
print(s)
print("%s,%%"%("Tom"))
print("%s,%")
print("%s,%%")
×Ö·û´®¸ñʽ¹æÔ¼
s = "The sword of truth"
print("[%.10s]" % (s))
print("%25s, %-25s," % (s, s))
import decimal
print("%s %r %a" % (decimal.Decimal("93.4"),
decimal.Decimal("93.4"),
decimal.Decimal("93.4")))
print("%s %r %a" % ("ÄãºÃ,ÊÀ½ç", "ÄãºÃ,ÊÀ½ç", "ÄãºÃ,ÊÀ½ç"))
ÕûÊý¸ñʽ¹æÔ¼
print("[%012d], [%-012d]"%(-8749203,-8749203))
print("[%+012d], [%012d]"%(8749203,8749203))
print("%d,%o,%x,%X,%c"%(255,255,255,255,255))
print("%#d,%#o,%#x,%#X,%#c"%(255,255,255,255,255))
¸¡µãÊý¸ñʽ¹æÔ¼
amount = (10 ** 3) * math.pi
print("[%12.2e], [%12.2f], [%12.2G]"%(amount,amount,amount))
str.format()¸ñʽ»¯
»ù±¾Ê¹ÓÃ
¿ÉÒÔʹÓÃ×Ö·û´®µÄformat()·½·¨·µ»Ø¸ñʽ»¯ºóµÄ×Ö·û´®,formatµÄ×Ö·û´®»ù±¾ÐÎʽΪ:
print("The novel '{0}' was published in {1}".format("Hard Times", 1854))
print("{{{0}}} {1} {{}}".format("I'm in braces", "I'm not"))
print("{who} turned {age} this year".format(who="She", age=88))
- »ìºÏʹÓÃλÖòÎÊýºÍ¹Ø¼ü×Ö²ÎÊý
print("The {who} was {0} last week".format(12, who="boy"))
stocks = ["paper", "envelopes", "notepads", "pens", "paper clips"]
print("We have {0[1]} and {0[2]} in stock".format(stocks))
d = {"animal": "elephant", "weight": 12000}
print("The {0[animal]} weighs {0[weight]}kg".format(d))
import sys
import math
print("math.pi == {0.pi}, sys.maxunicode == {1.maxunicode}".format(math, sys))
print("{} {} {}".format("Python", "can", "count"))
print("{0} {1} {0}".format(*[1, 2, 3]))
print("{x}".format(**{"x": 10, "y": 20}))
print("{x},{0},{1},{0}".format(*[1, 2, 3], **{"x": 10, "y": 20}))
print("{0} {0!s} {0!r} {0!a}".format(decimal.Decimal("93.4")))
print("{0} {0!s} {0!r} {0!a}".format("ÄãºÃ,ÊÀ½ç"))
×Ö·û´®¸ñʽ¹æÔ¼
s = "The sword of truth"
print("[{0:<25}, {0:>25}, {0:^25}]".format(s))
print("[{0:-<25}, {0:->25}, {0:-^25}]".format(s))
print("[{0:.{1}}]".format(s, 10))
ÕûÊý¸ñʽ¹æÔ¼
print("[{0:0<12},{0:0>12},{0:0^12},{0:0=12},{0:012}]".format(-8749203))
print("[{0:0=+5},{1:0=+5}]".format(500, -500))
print("[{0:0=-5},{1:0=-5}]".format(500, -500))
print("[{0:0= 5},{1:0= 5}]".format(500, -500))
print("{0:*>13,}".format(2394321))
print("{0:020b}, {0:o}, {0:x}, {0:X}, {0:d}, {0:c}".format(255))
print("{0:#020b}, {0:#o}, {0:#x}, {0:#X}, {0:d}, {0:c}".format(255))
import local
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
print("{0:n} {1:n}".format(123456, 1234.56))
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
print("{0:n} {1:n}".format(123456, 1234.56))
locale.setlocale(locale.LC_ALL, "C")
print("{0:n} {1:n}".format(123456, 1234.56))
¸¡µãÊý¸ñʽ¹æÔ¼
amount = (10 ** 3) * math.pi
print("{0:12.2e}, {0:12.2f}, {0:12.2g}".format(amount))
print("{0:12.2E}, {0:12.2%}, {0:12.2G}".format(amount))
f-string¸ñʽ»¯
»ù±¾Ê¹ÓÃ
f-string ÊÇPython3.6ÐÂÒýÈëµÄÒ»ÖÖ×Ö·û´®¸ñʽ»¯·½·¨¡£
print(f"{'She'} turned {88} this year")
who = "She"
age = 88
print(f"{who} turned {age} this year")
l = ["She",88]
print(f"{l[0]} turned {l[1]} this year")
d = {"who":"She","age":88}
print(f"{d['who']} turned {d['age']} this year")
import math
print(f"math.pi == {math.pi}")
age = 18
print(f"Age: {age+1}, weight:{65.0*1000}g")
print(f"2**2 = {pow(2,2)}")
a = "I'm in braces"
b = "I'm not"
print(f"{{{a}}} {b} {{}}")
s = '\u20ac'
print(f"{s}")
test = decimal.Decimal("93.4")
print(f"{test} {test!s} {test!r} {test!a}")
test = "ÄãºÃÊÀ½ç"
print(f"{test} {test!s} {test!r} {test!a}")
¸ñʽ¹æÔ¼
¸ñʽ¹æÔ¼·½·¨Óëstr.format()¸ñʽ»¯·½·¨µÄ¸ñʽ¹æÔ¼·½·¨»ù±¾Ò»ÖÂ
|