ITÊýÂë ¹ºÎï ÍøÖ· Í·Ìõ Èí¼þ ÈÕÀú ÔĶÁ ͼÊé¹Ý
TxTС˵ÔĶÁÆ÷
¡ýÓïÒôÔĶÁ,С˵ÏÂÔØ,¹ÅµäÎÄѧ¡ý
ͼƬÅúÁ¿ÏÂÔØÆ÷
¡ýÅúÁ¿ÏÂÔØͼƬ,ÃÀŮͼ¿â¡ý
ͼƬ×Ô¶¯²¥·ÅÆ÷
¡ýͼƬ×Ô¶¯²¥·ÅÆ÷¡ý
Ò»¼üÇå³ýÀ¬»ø
¡ýÇáÇáÒ»µã,Çå³ýϵͳÀ¬»ø¡ý
¿ª·¢: C++֪ʶ¿â Java֪ʶ¿â JavaScript Python PHP֪ʶ¿â È˹¤ÖÇÄÜ Çø¿éÁ´ ´óÊý¾Ý Òƶ¯¿ª·¢ ǶÈëʽ ¿ª·¢¹¤¾ß Êý¾Ý½á¹¹ÓëËã·¨ ¿ª·¢²âÊÔ ÓÎÏ·¿ª·¢ ÍøÂçЭÒé ϵͳÔËά
½Ì³Ì: HTML½Ì³Ì CSS½Ì³Ì JavaScript½Ì³Ì GoÓïÑÔ½Ì³Ì JQuery½Ì³Ì VUE½Ì³Ì VUE3½Ì³Ì Bootstrap½Ì³Ì SQLÊý¾Ý¿â½Ì³Ì CÓïÑÔ½Ì³Ì C++½Ì³Ì Java½Ì³Ì Python½Ì³Ì Python3½Ì³Ì C#½Ì³Ì
ÊýÂë: µçÄÔ ±Ê¼Ç±¾ ÏÔ¿¨ ÏÔʾÆ÷ ¹Ì̬ӲÅÌ Ó²ÅÌ ¶ú»ú ÊÖ»ú iphone vivo oppo СÃ× »ªÎª µ¥·´ ×°»ú ͼÀ­¶¡
 
   -> Python֪ʶ¿â -> Python×Ö·û´®¸ñʽ»¯ -> ÕýÎÄÔĶÁ

[Python֪ʶ¿â]Python×Ö·û´®¸ñʽ»¯

Python¸ñʽ»¯

²Î¿¼:¡¶Python3³ÌÐò¿ª·¢Ö¸ÄÏ µÚ¶þ°æ¡·

%¸ñʽ»¯

»ù±¾Ê¹ÓÃ

s = "name: %s, age: %d, weight: %fkg" % ("Tom", 20, 60.5)
print(s) # name: Tom, age: 20, weight: 60.500000kg

s = "name: %(name)s, age: %(age)d, weight: %(weight)fkg" % {
    "name": "Tom", "age": 20, "weight": 60.5
}
print(s) # name: Tom, age: 20, weight: 60.500000kg

# %ת»»
# print("%s,%"%("Tom")) ValueError: incomplete format
print("%s,%%"%("Tom")) # Tom,%
print("%s,%") # %s,%
print("%s,%%") # %s,%%

×Ö·û´®¸ñʽ¹æÔ¼

# ×Ö·û´®¸ñʽ¹æÔ¼·½·¨ [-] minwidth .maxwidth [s|r|a]
s = "The sword of truth"

# .maxwidthÖ¸¶¨×î´ó¿í¶È
print("[%.10s]" % (s)) # [The sword ]

# ĬÈÏÓÒ¶ÔÆë,Ìí¼Ó-×ó¶ÔÆë
print("%25s, %-25s," % (s, s)) # [       The sword of truth] [The sword of truth       ]

# sÊÇ×Ö·û´®ÐÎʽ,rÊDZíÏóÐÎʽ
# aÊÇasciiµÄ±íÏóÐÎʽ,×Ö·ûʹÓÃ7±ÈÌرíʾ,·ñÔòʹÓÃ×î¶ÌµÄ\xhh,\uhhhh»ò\Uhhhhhhhh
import decimal
print("%s %r %a" % (decimal.Decimal("93.4"), 
                    decimal.Decimal("93.4"),
                    decimal.Decimal("93.4"))) # 93.4 Decimal('93.4') Decimal('93.4')

# ÄãºÃ,ÊÀ½ç 'ÄãºÃ,ÊÀ½ç' '\u4f60\u597d\uff0c\u4e16\u754c'
print("%s %r %a" % ("ÄãºÃ,ÊÀ½ç", "ÄãºÃ,ÊÀ½ç", "ÄãºÃ,ÊÀ½ç"))

ÕûÊý¸ñʽ¹æÔ¼

# ÕûÊý¹æÔ¼·½·¨ [-] [+] # 0 minwidth

# ĬÈÏÓÒ¶ÔÆë,Ìí¼Ó-×ó¶ÔÆë,×ó¶ÔÆë²»»á0Ìî³ä
print("[%012d], [%-012d]"%(-8749203,-8749203)) # [-00008749203], [-8749203    ]

# +ºÅÕýÊýÇ¿ÖÆÌí¼Ó·ûºÅ
print("[%+012d], [%012d]"%(8749203,8749203)) # [+00008749203], [000008749203]

# dÊ®½øÖÆ,o°Ë½øÖÆ,xСдʮÁù½øÖÆ,X´óдʮÁù½øÖÆ,c¶ÔÓ¦×Ö·û
print("%d,%o,%x,%X,%c"%(255,255,255,255,255)) # 255,377,ff,FF,?

# o,x,X¿ÉÒÔÌí¼Ó#Ôö¼Ó½øÖÆÇ°µ¼·ûºÅ
print("%#d,%#o,%#x,%#X,%#c"%(255,255,255,255,255)) # 255,0o377,0xff,0XFF,?

¸¡µãÊý¸ñʽ¹æÔ¼

# ÓëÕûÊý»ù±¾Ò»Ñù,Ö»ÊÇÓÐÁ½¸ö²î±ð

# Ìí¼ÓÁË.precision¿ØÖÆСÊýµãµÄλÊý
# eСд×ÖĸָÊýÐÎʽ,E´óд×ÖĸָÊýÐÎʽ,f±ê×¼¸¡µã,gºÍGÊý×ÖСµÄʱºòÓëfÏàͬ,´óµÄʱºòÓëeºÍEÏàͬ
amount = (10 ** 3) * math.pi
# [    3.14e+03], [     3141.59], [     3.1E+03]
print("[%12.2e], [%12.2f], [%12.2G]"%(amount,amount,amount)) 

str.format()¸ñʽ»¯

»ù±¾Ê¹ÓÃ

¿ÉÒÔʹÓÃ×Ö·û´®µÄformat()·½·¨·µ»Ø¸ñʽ»¯ºóµÄ×Ö·û´®,formatµÄ×Ö·û´®»ù±¾ÐÎʽΪ:

# {field_name}
# {field_name!conversion}
# {field_name:format_specification}
# {field_name!conversion:format_specification}
  • ʹÓÃλÖòÎÊý
# The novel 'Hard Times' was published in 1854
print("The novel '{0}' was published in {1}".format("Hard Times", 1854))
  • ´óÀ¨ºÅµÄתÒå
# {I'm in braces} I'm not {}
print("{{{0}}} {1} {{}}".format("I'm in braces", "I'm not"))
  • ʹÓùؼü×Ö²ÎÊý
# She turned 88 this year
print("{who} turned {age} this year".format(who="She", age=88))
  • »ìºÏʹÓÃλÖòÎÊýºÍ¹Ø¼ü×Ö²ÎÊý
# ¹Ø¼ü×Ö²ÎÊý×ÜÔÚλÖòÎÊýÖ®ºó
# The boy was 12 last week
print("The {who} was {0} last week".format(12, who="boy"))
  • ʹÓÃÐòÁÐ
stocks = ["paper", "envelopes", "notepads", "pens", "paper clips"]
# We have envelopes and notepads in stock
print("We have {0[1]} and {0[2]} in stock".format(stocks))
  • ʹÓÃ×Öµä
d = {"animal": "elephant", "weight": 12000}
# ×Ö·û´®ÀïµÄ¼ü²»ÓÃÒýºÅ
# The elephant weighs 12000kg
print("The {0[animal]} weighs {0[weight]}kg".format(d))
  • ʹÓÃÊôÐÔ
import sys
import math
# math.pi == 3.141592653589793, sys.maxunicode == 1114111
print("math.pi == {0.pi}, sys.maxunicode == {1.maxunicode}".format(math, sys))
  • ºöÂÔ×Ö¶ÎÃû
# Python can count
print("{} {} {}".format("Python", "can", "count"))  # Python3.1Æð
  • ʹÓÃÐòÁвð·Ö
# 1 2 1
print("{0} {1} {0}".format(*[1, 2, 3]))
  • ʹÓÃÓ³Éä²ð·Ö
# Ö»ÓÐ×îºóÒ»¸ö²ÎÊý¿ÉÒÔʹÓÃÓ³Éä²ð·Ö
# 10
print("{x}".format(**{"x": 10, "y": 20}))
# 10,1,2,1
print("{x},{0},{1},{0}".format(*[1, 2, 3], **{"x": 10, "y": 20}))
  • ×Ö·û´®ÐÎʽת»»
# sÊÇ×Ö·û´®ÐÎʽ,rÊDZíÏóÐÎʽ
# aÊÇasciiµÄ±íÏóÐÎʽ,×Ö·ûʹÓÃ7±ÈÌرíʾ,·ñÔòʹÓÃ×î¶ÌµÄ\xhh,\uhhhh»ò\Uhhhhhhhh
# 93.4 93.4 Decimal('93.4') Decimal('93.4')
print("{0} {0!s} {0!r} {0!a}".format(decimal.Decimal("93.4")))
# ÄãºÃ,ÊÀ½ç ÄãºÃ,ÊÀ½ç 'ÄãºÃ,ÊÀ½ç' '\u4f60\u597d\uff0c\u4e16\u754c'
print("{0} {0!s} {0!r} {0!a}".format("ÄãºÃ,ÊÀ½ç"))

×Ö·û´®¸ñʽ¹æÔ¼

# ×Ö·û´®µÄ¸ñʽ¹æÔ¼ : fill[Ìî³ä] align[¶ÔÆë] miniwidth[×îС¿í¶È] .maxwidth[×î´ó¿í¶È]
s = "The sword of truth"

# ûÓÐÖ¸¶¨Ìî³ä×Ö·ûĬÈÏΪ¿Õ¸ñ,<×ó¶ÔÆë,>ÓÒ¶ÔÆë,^Öмä¶ÔÆë
# [The sword of truth       ,        The sword of truth,    The sword of truth    ]
print("[{0:<25}, {0:>25}, {0:^25}]".format(s))

# Ö¸¶¨ÁËÌî³ä×Ö·û±ØÐëÖ¸¶¨¶ÔÆë×Ö·û
# [The sword of truth-------, -------The sword of truth, ---The sword of truth----]
print("[{0:-<25}, {0:->25}, {0:-^25}]".format(s))

# ¸ñʽ¹æÔ¼ÄÚ²¿¿ÉÒÔ°üº¬Ìæ»»×Ö¶Î
# [The sword ]
print("[{0:.{1}}]".format(s, 10))

ÕûÊý¸ñʽ¹æÔ¼

# ÕûÊý¸ñʽ¹æÔ¼ : fill align sign # 0 minwidth , type(b c d n o x X)

# 0Ìî³ä,fill alignµÄÐÎʽ=»á½«·ûºÅÌáÇ°,ÆäËûµÄ²»»áÌáÇ°,0¹æÔ¼·ûºÅ»áÌáÇ°
# [-87492030000,0000-8749203,00-874920300,-00008749203,-00008749203]
print("[{0:0<12},{0:0>12},{0:0^12},{0:0=12},{0:012}]".format(-8749203))

# signµÄÑ¡Ïî+ - " "
# [+0500,-0500]
print("[{0:0=+5},{1:0=+5}]".format(500, -500))
# [00500,-0500]
print("[{0:0=-5},{1:0=-5}]".format(500, -500))
# [ 0500,-0500]
print("[{0:0= 5},{1:0= 5}]".format(500, -500))

# ,·Ö×é
# ****2,394,321
print("{0:*>13,}".format(2394321))

# type
# b¶þ½øÖÆ,o°Ë½øÖÆ,xСдʮÁù½øÖÆ,X´óдʮÁù½øÖÆ,¿ÉÒÔ¼Ó#ºÅ½ÐÉÏÇ°µ¼
# dÊ®½øÖÆ,c¶ÔÓ¦µÄunicode×ÖÔª,²»ÄܼÓ#
# 00000000000011111111, 377, ff, FF, 255, ?
print("{0:020b}, {0:o}, {0:x}, {0:X}, {0:d}, {0:c}".format(255))
# 0b000000000011111111, 0o377, 0xff, 0XFF, 255, ?
print("{0:#020b}, {0:#o}, {0:#x}, {0:#X}, {0:d}, {0:c}".format(255))

# nÒÔ³¡ËùÃô¸ÐµÄ·½Ê½Êä³öÊý×Ö,¶ÔÕûÊýĬÈÏdЧ¹û,¶Ô¸¡µãÊýĬÈÏgЧ¹û,ĬÈÏC³¡Ëù
import local

locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
# 123,456  1,234.56
print("{0:n}  {1:n}".format(123456, 1234.56))

locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
# 123.456  1.234,56
print("{0:n}  {1:n}".format(123456, 1234.56))

locale.setlocale(locale.LC_ALL, "C")
# 123456  1234.56
print("{0:n}  {1:n}".format(123456, 1234.56))

¸¡µãÊý¸ñʽ¹æÔ¼

# ¸¡µãÊý¸ñʽ¹æÔ¼¸¡µãÊý¸ñʽ¹æÔ¼ÓëÕûÊýÒ»Ñù,Ö»ÊÇÓÐÁ½¸ö²î±ð
# ¿ÉÒÔÖ¸¶¨¾«¶È(СÊýµãºó¸úËæµÄÊý×Ö¸öÊý)
# typeÖ¸¶¨eСд×ÖĸָÊýÐÎʽ,E´óд×ÖĸָÊýÐÎʽ,f±ê×¼¸¡µã
# gºÍGÊý×ÖСµÄʱºòÓëfÏàͬ,´óµÄʱºòÓëeºÍEÏàͬ,%°Ù·Ö±ÈÐÎʽ
amount = (10 ** 3) * math.pi
# [    3.14e+03,      3141.59,      3.1e+03]
print("{0:12.2e}, {0:12.2f}, {0:12.2g}".format(amount))
# [    3.14E+03,   314159.27%,      3.1E+03]
print("{0:12.2E}, {0:12.2%}, {0:12.2G}".format(amount))

f-string¸ñʽ»¯

»ù±¾Ê¹ÓÃ

f-stringÊÇPython3.6ÐÂÒýÈëµÄÒ»ÖÖ×Ö·û´®¸ñʽ»¯·½·¨¡£

  • »ù±¾ÐÎʽ
# {field_name}
# {field_name!conversion}
# {field_name:format_specification}
# {field_name!conversion:format_specification}
  • ʹÓÃ×ÖÃæÁ¿
# She turned 88 this year
print(f"{'She'} turned {88} this year")
  • ʹÓñäÁ¿
who = "She"
age = 88
# She turned 88 this year
print(f"{who} turned {age} this year")
  • ʹÓÃÐòÁÐ
l = ["She",88]
# She turned 88 this year
print(f"{l[0]} turned {l[1]} this year")
  • ʹÓÃ×Öµä
d = {"who":"She","age":88}
# She turned 88 this year
print(f"{d['who']} turned {d['age']} this year")
  • ʹÓÃÊôÐÔ
import math
# math.pi == 3.141592653589793
print(f"math.pi == {math.pi}")
  • ʹÓñí´ïʽ
age = 18
# Age: 19, weight:65000.0g
print(f"Age: {age+1}, weight:{65.0*1000}g")
  • ʹÓú¯Êý
# 2**2 = 4
print(f"2**2 = {pow(2,2)}")
  • ´óÀ¨ºÅתÒå
a = "I'm in braces"
b = "I'm not"
# {I'm in braces} I'm not {}
print(f"{{{a}}} {b} {{}}")
  • \תÒå
# ´óÀ¨ºÅÀï²»ÔÊÐí³öÏÖ`\`·ûºÅ
# print(f"{'\u20ac'}") SyntaxError: f-string expression part cannot include a backslash
# ʹÓñäÁ¿µÄÐÎʽ½â¾ö
s = '\u20ac'
# €
print(f"{s}")
  • ×Ö·û´®ÐÎʽת»»
# sÊÇ×Ö·û´®ÐÎʽ,rÊDZíÏóÐÎʽ
# aÊÇasciiµÄ±íÏóÐÎʽ,×Ö·ûʹÓÃ7±ÈÌرíʾ,·ñÔòʹÓÃ×î¶ÌµÄ\xhh,\uhhhh»ò\Uhhhhhhhh

test = decimal.Decimal("93.4")
# 93.4 93.4 Decimal('93.4') Decimal('93.4')
print(f"{test} {test!s} {test!r} {test!a}")

test = "ÄãºÃÊÀ½ç"
# ÄãºÃÊÀ½ç ÄãºÃÊÀ½ç 'ÄãºÃÊÀ½ç' '\u4f60\u597d\u4e16\u754c'
print(f"{test} {test!s} {test!r} {test!a}")

¸ñʽ¹æÔ¼

¸ñʽ¹æÔ¼·½·¨Óëstr.format()¸ñʽ»¯·½·¨µÄ¸ñʽ¹æÔ¼·½·¨»ù±¾Ò»ÖÂ

  Python֪ʶ¿â ×îÐÂÎÄÕÂ
PythonÖÐStringÄ£¿é
¡¾Python¡¿ 14-CVSÎļþ²Ù×÷
pythonµÄpanda¿â¶ÁдÎļþ
ʹÓÃNordicµÄnrf52840ʵÏÖÀ¶ÑÀDFU¹ý³Ì
¡¾Pythonѧϰ¼Ç¼¡¿numpyÊý×éÓ÷¨ÕûÀí
Pythonѧϰ±Ê¼Ç
python×Ö·û´®ºÍÁбí
pythonÈçºÎ´ÓtxtÎļþÖнâÎö³öÓÐЧµÄÊý¾Ý
Python±à³Ì´ÓÈëÃŵ½Êµ¼ù×Ôѧ/3.1-3.2
python±äÁ¿
ÉÏһƪÎÄÕ      ÏÂһƪÎÄÕ      ²é¿´ËùÓÐÎÄÕÂ
¼Ó:2022-01-30 18:53:50  ¸ü:2022-01-30 18:56:10 
 
¿ª·¢: C++֪ʶ¿â Java֪ʶ¿â JavaScript Python PHP֪ʶ¿â È˹¤ÖÇÄÜ Çø¿éÁ´ ´óÊý¾Ý Òƶ¯¿ª·¢ ǶÈëʽ ¿ª·¢¹¤¾ß Êý¾Ý½á¹¹ÓëËã·¨ ¿ª·¢²âÊÔ ÓÎÏ·¿ª·¢ ÍøÂçЭÒé ϵͳÔËά
½Ì³Ì: HTML½Ì³Ì CSS½Ì³Ì JavaScript½Ì³Ì GoÓïÑÔ½Ì³Ì JQuery½Ì³Ì VUE½Ì³Ì VUE3½Ì³Ì Bootstrap½Ì³Ì SQLÊý¾Ý¿â½Ì³Ì CÓïÑÔ½Ì³Ì C++½Ì³Ì Java½Ì³Ì Python½Ì³Ì Python3½Ì³Ì C#½Ì³Ì
ÊýÂë: µçÄÔ ±Ê¼Ç±¾ ÏÔ¿¨ ÏÔʾÆ÷ ¹Ì̬ӲÅÌ Ó²ÅÌ ¶ú»ú ÊÖ»ú iphone vivo oppo СÃ× »ªÎª µ¥·´ ×°»ú ͼÀ­¶¡

360ͼÊé¹Ý ¹ºÎï Èý·á¿Æ¼¼ ÔĶÁÍø ÈÕÀú ÍòÄêÀú 2024Äê11ÈÕÀú -2024/11/16 1:27:31-

ͼƬ×Ô¶¯²¥·ÅÆ÷
¡ýͼƬ×Ô¶¯²¥·ÅÆ÷¡ý
TxTС˵ÔĶÁÆ÷
¡ýÓïÒôÔĶÁ,С˵ÏÂÔØ,¹ÅµäÎÄѧ¡ý
Ò»¼üÇå³ýÀ¬»ø
¡ýÇáÇáÒ»µã,Çå³ýϵͳÀ¬»ø¡ý
ͼƬÅúÁ¿ÏÂÔØÆ÷
¡ýÅúÁ¿ÏÂÔØͼƬ,ÃÀŮͼ¿â¡ý
  ÍøÕ¾ÁªÏµ: qq:121756557 email:121756557@qq.com  ITÊýÂë