{"id":231810,"date":"2021-12-20T09:14:09","date_gmt":"2021-12-20T01:14:09","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231810"},"modified":"2021-12-13T09:14:34","modified_gmt":"2021-12-13T01:14:34","slug":"python-f-strings-formatting","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-f-strings-formatting.html","title":{"rendered":"Python\u7684f-strings\u683c\u5f0f\u5316"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b66\u8fc7Python\u7684\u670b\u53cb\u5e94\u8be5\u90fd\u77e5\u9053 f-strings \u662f\u7528\u6765\u975e\u5e38\u65b9\u4fbf\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u7684\uff0c\u89c9\u5f97\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u65e0\u5916\u4e4e\u5c31\u662fprint(f'value = { value }'\uff0c\u5176\u5b9e\uff0cf-strings\u8fdc\u8d85\u4f60\u7684\u9884\u671f\uff0c\u4eca\u5929\u6765\u68b3\u7406\u4e00\u4e0b\u5b83\u8fd8\u80fd\u505a\u90a3\u4e9b\u5f88\u9177\u7684\u4e8b\u60c5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5e38\u89c1\u7528\u6cd5<\/strong><\/div>\n

f-string\uff0c\u4ea6\u79f0\u4e3a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e38\u91cf\uff08formatted string literals\uff09\uff0c\u662fPython3.6\u65b0\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u6e90\u4e8ePEP 498 \u2013 Literal String Interpolation\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4f7f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u66f4\u52a0\u7b80\u4fbf\u3002f-string\u5728\u5f62\u5f0f\u4e0a\u662f\u4ee5 f \u6216 F \u4fee\u9970\u7b26\u5f15\u9886\u7684\u5b57\u7b26\u4e32\uff08f'xxx'\u6216 F'xxx'\uff09\uff0c\u4ee5\u5927\u62ec\u53f7 {} \u6807\u660e\u88ab\u66ff\u6362\u7684\u5b57\u6bb5\uff1bf-string\u5728\u672c\u8d28\u4e0a\u5e76\u4e0d\u662f\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u800c\u662f\u4e00\u4e2a\u5728\u8fd0\u884c\u65f6\u8fd0\u7b97\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n

1\u3001\u61d2\u5f97\u518d\u6572\u4e00\u904d\u53d8\u91cf\u540d<\/strong><\/span><\/div>\n
\r\nstr_value = \"hello\uff0cpython coders\"   \r\nprint(f\"{ str_value = }\")    \r\n# str_value = 'hello\uff0cpython coders'<\/pre>\n
2\u3001\u76f4\u63a5\u6539\u53d8\u8f93\u51fa\u7ed3\u679c<\/strong><\/span><\/div>\n
num_value = 123    \r\nprint(f\"{num_value % 2 = }\")    \r\n# num_value % 2 = 1<\/pre>\n
3\u3001\u76f4\u63a5\u683c\u5f0f\u5316\u65e5\u671f <\/strong><\/span><\/div>\n
import datetime   \r\ntoday = datetime.date.today()   \r\nprint(f\"{today: %Y%m%d}\")    \r\n# 20211019    \r\nprint(f\"{today =: %Y%m%d}\")  \r\n# today = 20211019<\/pre>\n
4\u30012\/8\/16 \u8fdb\u5236\u8f93\u51fa\u771f\u7684\u592a\u7b80\u5355 <\/strong><\/span><\/div>\n
>>> a = 42    \r\n>>> f\"{a:b}\" # 2\u8fdb\u5236    \r\n'101010'    \r\n>>> f\"{a:o}\" # 8\u8fdb\u5236    \r\n'52'    \r\n>>> f\"{a:x}\" # 16\u8fdb\u5236\uff0c\u5c0f\u5199\u5b57\u6bcd    \r\n'2a'    \r\n>>> f\"{a:X}\" # 16\u8fdb\u5236\uff0c\u5927\u5199\u5b57\u6bcd    \r\n'2A'    \r\n>>> f\"{a:c}\" # ascii \u7801    \r\n'*'  <\/pre>\n
5\u3001\u683c\u5f0f\u5316\u6d6e\u70b9\u6570<\/strong><\/span><\/div>\n
\r\n>>> num_value = 123.456    \r\n>>> f'{num_value = :.2f}' #\u4fdd\u7559 2 \u4f4d\u5c0f\u6570    \r\n'num_value = 123.46'    \r\n>>> nested_format = \".2f\" #\u53ef\u4ee5\u4f5c\u4e3a\u53d8\u91cf    \r\n>>> print(f'{num_value:{nested_format}}')    \r\n123.46<\/pre>\n
6\u3001\u5b57\u7b26\u4e32\u5bf9\u9f50\uff0cso easy\uff01<\/strong><\/span><\/div>\n
>>> x = 'test'    \r\n>>> f'{x:>10}'   # \u53f3\u5bf9\u9f50\uff0c\u5de6\u8fb9\u8865\u7a7a\u683c    \r\n'      test'    \r\n>>> f'{x:*<10}'  # \u5de6\u5bf9\u9f50\uff0c\u53f3\u8fb9\u8865*    \r\n'test******'    \r\n>>> f'{x:=^10}'  # \u5c45\u4e2d\uff0c\u5de6\u53f3\u8865=    \r\n'===test==='    \r\n>>> x, n = 'test', 10    \r\n>>> f'{x:~^{n}}' # \u53ef\u4ee5\u4f20\u5165\u53d8\u91cf n    \r\n'~~~test~~~'    \r\n>>><\/pre>\n
7\u3001\u4f7f\u7528 !s\uff0c!r <\/strong><\/span><\/div>\n
>>> x = '\u4e2d'    \r\n>>> f\"{x!s}\" # \u76f8\u5f53\u4e8e str(x) \r\n'\u4e2d'    \r\n>>> f\"{x!r}\" # \u76f8\u5f53\u4e8e repr(x)    \r\n\"'\u4e2d'\"<\/pre>\n
8\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f <\/strong><\/span><\/div>\n
class MyClass:    \r\n    def __format__(self, format_spec) -> str:   \r\n        print(f'MyClass __format__ called with {format_spec=!r}')  \r\n        return \"MyClass()\"      \r\nprint(f'{MyClass():bala bala  %%MYFORMAT%%}')<\/pre>\n

\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n

\r\nMyClass __format__ called with format_spec='bala bala  %%MYFORMAT%%'    \r\nMyClass() <\/pre>\n
\u6700\u540e<\/strong><\/div>\n

Python \u7684 f-string \u975e\u5e38\u7075\u6d3b\u4f18\u96c5\uff0c\u540c\u65f6\u8fd8\u662f\u6548\u7387\u6700\u9ad8\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u5f0f\uff1a
\n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

f-string\uff0c\u4ea6\u79f0\u4e3a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e38\u91cf\uff08formatted string literals\uff09\uff0c\u662fPython […]<\/p>\n","protected":false},"author":1329,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231810"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231810\/revisions"}],"predecessor-version":[{"id":231817,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231810\/revisions\/231817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}