{"id":1145497,"date":"2025-01-08T23:08:43","date_gmt":"2025-01-08T15:08:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145497.html"},"modified":"2025-01-08T23:08:46","modified_gmt":"2025-01-08T15:08:46","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8d%e8%be%93%e5%87%ba%e5%9c%a8%e4%b8%80%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145497.html","title":{"rendered":"python\u5982\u4f55\u4e0d\u8f93\u51fa\u5728\u4e00\u884c\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182043\/124b3978-5076-4c06-8df5-3ce8f8f2c5ca.webp\" alt=\"python\u5982\u4f55\u4e0d\u8f93\u51fa\u5728\u4e00\u884c\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u907f\u514d\u5728\u4e00\u884c\u8f93\u51fa\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u4f7f\u7528print\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u6807\u51c6\u5e93\u51fd\u6570\u7b49\u3002<\/strong> \u5176\u4e2d\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>print<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u5176\u4e2d\u52a0\u5165<code>end<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u8f93\u51fa\u65f6\u7684\u7ed3\u675f\u7b26\u53f7\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6570\u636e\u7c7b\u578b\u51fa\u73b0\u5728\u540c\u4e00\u884c\u8f93\u51fa\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u6216\u8005\u5229\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u7684\u683c\u5f0f\u5316\u4e0e\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528print\u51fd\u6570\u548cend\u53c2\u6570<\/strong>\u662f\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6307\u5b9a<code>end<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u51b3\u5b9a\u5728\u6bcf\u6b21<code>print<\/code>\u8f93\u51fa\u540e\u63a5\u4ec0\u4e48\u5b57\u7b26\uff0c\u9ed8\u8ba4\u662f\u6362\u884c\u7b26\uff08<code>\\n<\/code>\uff09\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u8fde\u7eed\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, end=&quot; &quot;)<\/p>\n<p>print(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u8f93\u51fa\u201cHello World\u201d\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u6362\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570\u63a7\u5236\u8f93\u51fa<\/h3>\n<\/p>\n<p><p><code>print<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8f93\u51fa\u51fd\u6570\u3002\u5b83\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8f93\u51fa\u5185\u5bb9\u5e76\u6362\u884c\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>end<\/code>\u53c2\u6570\u6765\u6539\u53d8\u8fd9\u4e00\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>print<\/code>\u51fd\u6570\u4f1a\u5728\u8f93\u51fa\u5185\u5bb9\u540e\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;)<\/p>\n<p>print(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello<\/p>\n<p>World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528end\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>end<\/code>\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u6bcf\u6b21<code>print<\/code>\u8f93\u51fa\u540e\u7684\u7ed3\u675f\u7b26\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, end=&quot;&quot;)<\/p>\n<p>print(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>HelloWorld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5176\u4ed6\u5b57\u7b26\uff0c\u4f8b\u5982\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, end=&quot; &quot;)<\/p>\n<p>print(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed3\u5408\u5176\u4ed6\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u53c2\u6570\u4f7f\u7528\uff0c\u4f8b\u5982<code>sep<\/code>\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u591a\u4e2a\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, &quot;World&quot;, sep=&quot;, &quot;, end=&quot;!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63a7\u5236\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002Python\u4e2d\u6709\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001<code>str.format()<\/code>\u65b9\u6cd5\u548cf\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u8f83\u4e3a\u53e4\u8001\uff0c\u4f46\u4ecd\u7136\u5728\u4e00\u4e9b\u4ee3\u7801\u4e2d\u88ab\u4f7f\u7528\u3002\u901a\u8fc7\u4f7f\u7528<code>%<\/code>\u7b26\u53f7\u548c\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: %s, Age: %d&quot; % (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001str.format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u82b1\u62ec\u53f7<code>{}<\/code>\u548c\u53d8\u91cf\u540d\u6765\u66ff\u4ee3\u5360\u4f4d\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Name: {0}, Age: {1}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h4>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>f<\/code>\u6216<code>F<\/code>\uff0c\u5e76\u5728\u82b1\u62ec\u53f7\u5185\u76f4\u63a5\u5199\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u76f4\u89c2\u7684\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u548c\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pi = 3.141592653589793<\/p>\n<p>print(f&quot;Pi rounded to 2 decimal places: {pi:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Pi rounded to 2 decimal places: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u51fd\u6570\u63a7\u5236\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u548c\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982<code>pprint<\/code>\u6a21\u5757\u7528\u4e8e\u7f8e\u5316\u6253\u5370\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c<code>json<\/code>\u6a21\u5757\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001pprint\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>pprint<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>pprint<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u7f8e\u5316\u6253\u5370\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5d4c\u5957\u5217\u8868\u6216\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pprint<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;]}<\/p>\n<p>pprint.pprint(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>{&#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;], &#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001json\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>dumps<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5e8f\u5217\u5316Python\u5bf9\u8c61\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5e76\u901a\u8fc7<code>indent<\/code>\u53c2\u6570\u5b9e\u73b0\u7f8e\u5316\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;]}<\/p>\n<p>print(json.dumps(data, indent=4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;hobbies&quot;: [<\/p>\n<p>        &quot;reading&quot;,<\/p>\n<p>        &quot;hiking&quot;,<\/p>\n<p>        &quot;coding&quot;<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6240\u9700\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5229\u7528\u4e0a\u8ff0\u65b9\u6cd5\u63a7\u5236\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;]}<\/p>\n<h2><strong>\u4f7f\u7528print\u51fd\u6570\u548cend\u53c2\u6570<\/strong><\/h2>\n<p>print(&quot;User Information:&quot;, end=&quot; &quot;)<\/p>\n<p>print(data)<\/p>\n<h2><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>name = data[&#39;name&#39;]<\/p>\n<p>age = data[&#39;age&#39;]<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pprint\u6a21\u5757\u7f8e\u5316\u6253\u5370<\/strong><\/h2>\n<p>import pprint<\/p>\n<p>pprint.pprint(data)<\/p>\n<h2><strong>\u4f7f\u7528json\u6a21\u5757\u7f8e\u5316\u6253\u5370<\/strong><\/h2>\n<p>print(json.dumps(data, indent=4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>User Information: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;]}<\/p>\n<p>Name: Alice, Age: 30<\/p>\n<p>{&#39;age&#39;: 30, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;hiking&#39;, &#39;coding&#39;], &#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;hobbies&quot;: [<\/p>\n<p>        &quot;reading&quot;,<\/p>\n<p>        &quot;hiking&quot;,<\/p>\n<p>        &quot;coding&quot;<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5229\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u907f\u514d\u5728\u4e00\u884c\u8f93\u51fa\u6570\u636e\u7c7b\u578b\u3002\u540c\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e5f\u5c55\u793a\u4e86\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u7075\u6d3b\u5e94\u7528\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u548c\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u907f\u514d\u6570\u636e\u7c7b\u578b\u5728\u4e00\u884c\u663e\u793a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6362\u884c\u7b26 <code>\\n<\/code> \u6765\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002\u5982\u679c\u5e0c\u671b\u6bcf\u4e2a\u6570\u636e\u7c7b\u578b\u5355\u72ec\u5360\u4e00\u884c\uff0c\u53ef\u4ee5\u5728\u6253\u5370\u65f6\u52a0\u5165\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [1, 2.5, &quot;Hello&quot;, True]\nfor item in data:\n    print(item)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\u4f1a\u5c06\u6bcf\u4e2a\u6570\u636e\u7c7b\u578b\u5206\u522b\u8f93\u51fa\u5728\u4e0d\u540c\u7684\u884c\u4e0a\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8f93\u51fa\u683c\u5f0f\u5316\u4e3a\u66f4\u6613\u8bfb\u7684\u5f62\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216f-string\u6765\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u5176\u66f4\u52a0\u6613\u8bfb\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [1, 2.5, &quot;Hello&quot;, True]\nfor item in data:\n    print(f&quot;\u6570\u636e\u7c7b\u578b: {type(item).__name__}, \u503c: {item}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u8f93\u51fa\u6570\u636e\u7684\u7c7b\u578b\uff0c\u8fd8\u80fd\u5c06\u5176\u503c\u6e05\u6670\u5730\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8f93\u51fa\u65f6\u9690\u85cf\u67d0\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u8be6\u7ec6\u4fe1\u606f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u8f93\u51fa\u903b\u8f91\u6765\u9009\u62e9\u6027\u5730\u663e\u793a\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u8f93\u51fa\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [1, 2.5, &quot;Hello&quot;, True]\nfor item in data:\n    if isinstance(item, (int, float)):\n        print(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u8df3\u8fc7\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\uff0c\u53ea\u8f93\u51fa\u6570\u5b57\u7c7b\u578b\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u4e13\u6ce8\u4e8e\u4ed6\u4eec\u5173\u5fc3\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u907f\u514d\u5728\u4e00\u884c\u8f93\u51fa\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u4f7f\u7528print\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u6807\u51c6\u5e93\u51fd\u6570\u7b49\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1145509,"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\/1145497"}],"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=1145497"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145497\/revisions"}],"predecessor-version":[{"id":1145513,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145497\/revisions\/1145513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145509"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}