{"id":1043996,"date":"2024-12-31T13:10:53","date_gmt":"2024-12-31T05:10:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043996.html"},"modified":"2024-12-31T13:10:55","modified_gmt":"2024-12-31T05:10:55","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e6%88%90%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043996.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u5316\u6210\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/84df58f8-7d90-46c9-bcca-d6866ffd83f1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python \u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528str()\u51fd\u6570\u3001repr()\u51fd\u6570\u3001format()\u65b9\u6cd5\u3001f-string\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528str()\u51fd\u6570\u548crepr()\u51fd\u6570\u3002str()\u51fd\u6570\u5c06\u5bf9\u8c61\u8f6c\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u800crepr()\u51fd\u6570\u5219\u5c06\u5bf9\u8c61\u8f6c\u5316\u4e3a\u89e3\u91ca\u5668\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528format()\u65b9\u6cd5\u548cf-string\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528str()\u51fd\u6570<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002str()\u51fd\u6570\u7528\u4e8e\u5c06\u6570\u503c\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49Python\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528str(123)\uff0c\u5b83\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32\u201c123\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001str()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>str()\u51fd\u6570\u662f\u5c06Python\u4e2d\u7684\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u60c5\u51b5\uff0c\u65e0\u8bba\u662f\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u8fd8\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u57fa\u672c\u7684str()\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u503c\u548c\u5176\u4ed6\u7b80\u5355\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>string = str(number)<\/p>\n<p>print(string)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8f6c\u6362\u590d\u6742\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>str()\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u8f6c\u6362\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u5217\u8868\u548c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>list_str = str(my_list)<\/p>\n<p>dict_str = str(my_dict)<\/p>\n<p>print(list_str)  # \u8f93\u51fa: &quot;[1, 2, 3]&quot;<\/p>\n<p>print(dict_str)  # \u8f93\u51fa: &quot;{&#39;a&#39;: 1, &#39;b&#39;: 2}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001repr()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>repr()\u51fd\u6570\u7528\u4e8e\u751f\u6210\u89e3\u91ca\u5668\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u8c03\u8bd5\u548c\u8bb0\u5f55\u65e5\u5fd7\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u6bd4str()\u51fd\u6570\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>repr()\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u4e0estr()\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4ea7\u751f\u7684\u5b57\u7b26\u4e32\u66f4\u9002\u5408\u5728\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>string = repr(number)<\/p>\n<p>print(string)  # \u8f93\u51fa: &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8f6c\u6362\u590d\u6742\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>repr()\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u5217\u8868\u548c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>list_repr = repr(my_list)<\/p>\n<p>dict_repr = repr(my_dict)<\/p>\n<p>print(list_repr)  # \u8f93\u51fa: &quot;[1, 2, 3]&quot;<\/p>\n<p>print(dict_repr)  # \u8f93\u51fa: &quot;{&#39;a&#39;: 1, &#39;b&#39;: 2}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u82b1\u62ec\u53f7{}\u6765\u63d2\u5165\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\uff0c\u751f\u6210\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528format()\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>formatted_string = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;Name: Alice, Age: 30&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>format()\u65b9\u6cd5\u8fd8\u652f\u6301\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u6bd4\u5982\u6307\u5b9a\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 123.456789<\/p>\n<p>formatted_string = &quot;Value: {:.2f}&quot;.format(value)<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;Value: 123.46&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cf\u201d\uff0c\u5e76\u4f7f\u7528\u82b1\u62ec\u53f7{}\u6765\u63d2\u5165\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\u3002f-string\u6bd4format()\u65b9\u6cd5\u66f4\u7b80\u6d01\u3001\u66f4\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528f-string\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 25<\/p>\n<p>formatted_string = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;Name: Bob, Age: 25&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>f-string\u540c\u6837\u652f\u6301\u590d\u6742\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u6bd4\u5982\u6307\u5b9a\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 987.654321<\/p>\n<p>formatted_string = f&quot;Value: {value:.2f}&quot;<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;Value: 987.65&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b57\u7b26\u4e32\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0__str__()\u548c__repr__()\u65b9\u6cd5\u6765\u5b9a\u4e49\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u8fd9\u4f7f\u5f97\u5bf9\u8c61\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u8be6\u7ec6\u548c\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9e\u73b0__str__()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><strong>str<\/strong>()\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u201c\u4eba\u7c7b\u53ef\u8bfb\u201d\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;Person(name={self.name}, age={self.age})&quot;<\/p>\n<p>person = Person(&quot;Charlie&quot;, 40)<\/p>\n<p>print(str(person))  # \u8f93\u51fa: &quot;Person(name=Charlie, age=40)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0__repr__()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><strong>repr<\/strong>()\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u201c\u89e3\u91ca\u5668\u53ef\u8bfb\u201d\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;Person(name={self.name!r}, age={self.age!r})&quot;<\/p>\n<p>person = Person(&quot;David&quot;, 50)<\/p>\n<p>print(repr(person))  # \u8f93\u51fa: &quot;Person(name=&#39;David&#39;, age=50)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u548c\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;World&quot;<\/p>\n<p>combined_str = str1 + &quot; &quot; + str2<\/p>\n<p>print(combined_str)  # \u8f93\u51fa: &quot;Hello World&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528join()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>join()\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u8fde\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;Hello&quot;, &quot;World&quot;, &quot;Python&quot;]<\/p>\n<p>combined_str = &quot; &quot;.join(str_list)<\/p>\n<p>print(combined_str)  # \u8f93\u51fa: &quot;Hello World Python&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u548c\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u6216\u6587\u4ef6\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\uff0c\u5982UTF-8\u3001ASCII\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u7b26\u4e32\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u7801\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>encoded_string = string.encode(&quot;utf-8&quot;)<\/p>\n<p>print(encoded_string)  # \u8f93\u51fa: b&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u7b26\u4e32\u89e3\u7801<\/h4>\n<\/p>\n<p><p>\u89e3\u7801\u662f\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">encoded_string = b&#39;Hello, World!&#39;<\/p>\n<p>decoded_string = encoded_string.decode(&quot;utf-8&quot;)<\/p>\n<p>print(decoded_string)  # \u8f93\u51fa: &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u683c\u5f0f\u5316\u65f6\uff0cPython\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u6a21\u677f\u5b57\u7b26\u4e32\u548c\u683c\u5f0f\u5316\u89c4\u8303\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\uff08Template String\uff09\u5141\u8bb8\u4f7f\u7528\u5360\u4f4d\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Hello, $name!&quot;)<\/p>\n<p>formatted_string = template.substitute(name=&quot;Alice&quot;)<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;Hello, Alice!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u89c4\u8303\uff08Format Specification\uff09\u63d0\u4f9b\u4e86\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u6bd4\u5982\u5bf9\u9f50\u3001\u586b\u5145\u548c\u6570\u503c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 42<\/p>\n<p>formatted_string = &quot;{:&gt;10}&quot;.format(value)<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: &quot;        42&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5904\u7406\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u548c\u53cd\u659c\u6760\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8f6c\u4e49\u5b57\u7b26\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5e38\u7528\u8f6c\u4e49\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5e38\u7528\u7684\u8f6c\u4e49\u5b57\u7b26\u5305\u62ec\u6362\u884c\u7b26\uff08\\n\uff09\u3001\u5236\u8868\u7b26\uff08\\t\uff09\u548c\u53cd\u659c\u6760\uff08\\\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello\\nWorld!\\tPython\\\\Rocks!&quot;<\/p>\n<p>print(string)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Hello<\/strong><\/h2>\n<h2><strong>World!   Python\\Rocks!<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u539f\u59cb\u5b57\u7b26\u4e32\uff08Raw String\uff09\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cr\u201d\u6765\u907f\u514d\u8f6c\u4e49\u5b57\u7b26\u7684\u5904\u7406\uff0c\u5e38\u7528\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_string = r&quot;Hello\\nWorld!\\tPython\\\\Rocks!&quot;<\/p>\n<p>print(raw_string)<\/p>\n<h2><strong>\u8f93\u51fa: Hello\\nWorld!\\tPython\\\\Rocks!<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u63d0\u9ad8\u5b57\u7b26\u4e32\u64cd\u4f5c\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5217\u8868\u4ee3\u66ff\u5b57\u7b26\u4e32\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u9891\u7e41\u8fde\u63a5\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528\u5217\u8868\u548cjoin()\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = []<\/p>\n<p>for i in range(100000):<\/p>\n<p>    str_list.append(str(i))<\/p>\n<p>combined_str = &quot;&quot;.join(str_list)<\/p>\n<p>print(combined_str[:100])  # \u8f93\u51fa: &quot;0123456789...&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u8282\u7701\u5185\u5b58\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">combined_str = &quot;&quot;.join(str(i) for i in range(100000))<\/p>\n<p>print(combined_str[:100])  # \u8f93\u51fa: &quot;0123456789...&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1str()\u51fd\u6570\u3001repr()\u51fd\u6570\u3001format()\u65b9\u6cd5\u3001f-string\u4ee5\u53ca\u5176\u4ed6\u9ad8\u7ea7\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u64cd\u4f5c\u9700\u6c42\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u53ef\u4ee5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u63d0\u9ad8\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u5c06\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u51fa\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\u548c\u53ef\u8bfb\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u5c06\u51e0\u4e4e\u6240\u6709\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u53ea\u9700\u5c06\u6570\u636e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>str()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">num = 123\nstr_num = str(num)  # &#39;123&#39;\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u548c\u89e3\u7801\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u7f16\u7801\u548c\u89e3\u7801\u5728Python\u4e2d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\uff0c\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_str = &quot;Hello, World!&quot;\nbyte_str = original_str.encode(&#39;utf-8&#39;)  # \u7f16\u7801\u4e3a\u5b57\u8282\u4e32\ndecoded_str = byte_str.decode(&#39;utf-8&#39;)  # \u89e3\u7801\u56de\u5b57\u7b26\u4e32\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>f-string<\/code>\u3001<code>str.format()<\/code>\u548c\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u3002\u4f7f\u7528<code>f-string<\/code>\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u63d2\u5165\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nformatted_str = f&quot;My name is {name} and I am {age} years old.&quot;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u4f7f\u5f97\u5b57\u7b26\u4e32\u7684\u6784\u5efa\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528str()\u51fd\u6570\u3001repr()\u51fd\u6570\u3001format()\u65b9\u6cd5\u3001f-s [&hellip;]","protected":false},"author":3,"featured_media":1044003,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043996"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1043996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043996\/revisions"}],"predecessor-version":[{"id":1044007,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043996\/revisions\/1044007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044003"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}