{"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":"
\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 |