{"id":923299,"date":"2024-12-26T14:47:14","date_gmt":"2024-12-26T06:47:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/923299.html"},"modified":"2024-12-26T14:47:17","modified_gmt":"2024-12-26T06:47:17","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%95%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/923299.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f15\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211426\/a4a94751-206d-4067-8922-1dd373d916b4.webp\" alt=\"python\u5982\u4f55\u6253\u5f15\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6253\u5f15\u53f7\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u3001\u4e09\u5f15\u53f7\u3002<\/strong>\u8fd9\u4e9b\u5f15\u53f7\u5728Python\u4e2d\u6709\u4e0d\u540c\u7684\u7528\u9014\uff0c\u4f8b\u5982\uff0c\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u7528\u4e8e\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u800c\u4e09\u5f15\u53f7\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u6216\u6587\u6863\u5b57\u7b26\u4e32\u3002<strong>\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u7684\u4f7f\u7528\u6ca1\u6709\u533a\u522b\uff0c\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528<\/strong>\u3002\u5f53\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5f15\u53f7\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08\\\uff09\u6765\u907f\u514d\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong>\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u5728Python\u4e2d\u662f\u7b49\u6548\u7684\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u6839\u636e\u5b57\u7b26\u4e32\u5185\u5bb9\u7684\u9700\u8981\u6765\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u79cd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u53cc\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6765\u5305\u88f9\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5728\u5904\u7406\u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002\u6b64\u5916\uff0cPython\u8fd8\u652f\u6301\u4f7f\u7528\u4e09\u5f15\u53f7\uff08\u5355\u5f15\u53f7&#39;&#39;&#39;\u6216\u53cc\u5f15\u53f7&quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u8fd9\u5728\u4e66\u5199\u957f\u6587\u672c\u6216\u6587\u6863\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u4f7f\u7528\u5f15\u53f7\u7684\u4e0d\u540c\u573a\u666f\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5355\u5f15\u53f7\u4e0e\u53cc\u5f15\u53f7<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u5355\u5f15\u53f7\uff08&#39;\uff09\u548c\u53cc\u5f15\u53f7\uff08&quot;\uff09\u7528\u4e8e\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002\u4e24\u8005\u7684\u529f\u80fd\u76f8\u540c\uff0c\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u5f15\u53f7\u901a\u5e38\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u5f15\u53f7\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u90fd\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5355\u5f15\u53f7<\/p>\n<p>string1 = &#39;Hello, World!&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u53cc\u5f15\u53f7<\/strong><\/h2>\n<p>string2 = &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.2 \u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5f15\u53f7\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u79cd\u5f15\u53f7\u6765\u907f\u514d\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u53cc\u5f15\u53f7<\/p>\n<p>string3 = &#39;He s<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>d, &quot;Hello, World!&quot;&#39;<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5355\u5f15\u53f7<\/strong><\/h2>\n<p>string4 = &quot;It&#39;s a beautiful day!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.3 \u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u9700\u8981\u5305\u542b\u4e0e\u5916\u5c42\u5f15\u53f7\u76f8\u540c\u7684\u5f15\u53f7\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u8fdb\u884c\u8f6c\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/p>\n<p>string5 = &#39;It\\&#39;s a beautiful day!&#39;<\/p>\n<p>string6 = &quot;He said, \\&quot;Hello, World!\\&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u591a\u884c\u5b57\u7b26\u4e32\u4e0e\u6587\u6863\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p><p>\u4e09\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u7528\u4e8e\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u6216\u6587\u6863\u5b57\u7b26\u4e32\u3002\u591a\u884c\u5b57\u7b26\u4e32\u53ef\u4ee5\u8de8\u8d8a\u591a\u884c\uff0c\u800c\u6587\u6863\u5b57\u7b26\u4e32\u901a\u5e38\u7528\u4e8e\u4e3a\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u6dfb\u52a0\u6ce8\u91ca\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u591a\u884c\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u591a\u884c\u5b57\u7b26\u4e32\u5728\u9700\u8981\u5c06\u957f\u6587\u672c\u5206\u6210\u591a\u884c\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &#39;&#39;&#39;This is a<\/p>\n<p>multi-line string<\/p>\n<p>in Python.&#39;&#39;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2 \u6587\u6863\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u7528\u4e8e\u4e3a\u4ee3\u7801\u6dfb\u52a0\u8bf4\u660e\u6027\u6ce8\u91ca\uff0c\u901a\u5e38\u5728\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u7684\u5f00\u5934\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function():<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is a docstring for the example_function.<\/p>\n<p>    It provides an explanation of what the function does.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5b57\u7b26\u4e32\u4e2d\u7684\u5176\u4ed6\u8f6c\u4e49\u5b57\u7b26<\/strong><\/h2>\n<p><p>\u9664\u4e86\u5f15\u53f7\uff0cPython\u8fd8\u652f\u6301\u5176\u4ed6\u8f6c\u4e49\u5b57\u7b26\uff0c\u7528\u4e8e\u8868\u793a\u7279\u6b8a\u5b57\u7b26\uff0c\u4f8b\u5982\u6362\u884c\u7b26\uff08\\n\uff09\u3001\u5236\u8868\u7b26\uff08\\t\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u5e38\u89c1\u8f6c\u4e49\u5b57\u7b26<\/h2>\n<\/p>\n<ul>\n<li>\u6362\u884c\u7b26\uff1a<code>\\n<\/code><\/li>\n<li>\u5236\u8868\u7b26\uff1a<code>\\t<\/code><\/li>\n<li>\u53cd\u659c\u6760\uff1a<code>\\<\/code><\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u8fd9\u4e9b\u8f6c\u4e49\u5b57\u7b26\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\u793a\u4f8b<\/p>\n<p>text = &quot;First line.\\nSecond line.\\tIndented text.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2 \u539f\u59cb\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5b57\u7b26\u4e32\u4e2d\u7684\u53cd\u659c\u6760\u4e0d\u88ab\u89e3\u91ca\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>r<\/code>\u6216<code>R<\/code>\u6765\u5b9a\u4e49\u539f\u59cb\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5b57\u7b26\u4e32\u793a\u4f8b<\/p>\n<p>raw_string = r&quot;C:\\Users\\Name\\Documents&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684printf\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>formatted_string = &quot;Name: %s, Age: %d&quot; % (name, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4.2 \u4f7f\u7528str.format()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_string = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4.3 \u4f7f\u7528f\u5b57\u7b26\u4e32\uff08Python 3.6+\uff09<\/h2>\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\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_string = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u62fc\u63a5\u3001\u5207\u7247\u3001\u66ff\u6362\u7b49\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u5b57\u7b26\u4e32\u62fc\u63a5<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u6216join\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;World&quot;<\/p>\n<p>concatenated = str1 + &quot; &quot; + str2<\/p>\n<h2><strong>\u4f7f\u7528join\u65b9\u6cd5<\/strong><\/h2>\n<p>concatenated = &quot; &quot;.join([str1, str2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.2 \u5b57\u7b26\u4e32\u5207\u7247<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u7528\u4e8e\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello, World!&quot;<\/p>\n<p>substring = s[7:12]  # \u63d0\u53d6&quot;World&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.3 \u5b57\u7b26\u4e32\u66ff\u6362<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;Hello, World!&quot;<\/p>\n<p>new_s = s.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5b57\u7b26\u4e32\u5e38\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u65b9\u6cd5\u6765\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u5927\u5c0f\u5199\u8f6c\u6362<\/h2>\n<\/p>\n<ul>\n<li><code>upper()<\/code>: \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/li>\n<li><code>lower()<\/code>: \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002<\/li>\n<li><code>capitalize()<\/code>: \u5c06\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">s = &quot;hello, world!&quot;<\/p>\n<p>print(s.upper())  # \u8f93\u51fa: HELLO, WORLD!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.2 \u67e5\u627e\u548c\u8ba1\u6570<\/h2>\n<\/p>\n<ul>\n<li><code>find()<\/code>: \u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><code>count()<\/code>: \u8ba1\u7b97\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">s = &quot;hello, world!&quot;<\/p>\n<p>position = s.find(&quot;world&quot;)<\/p>\n<p>count = s.count(&quot;l&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.3 \u62c6\u5206\u548c\u8fde\u63a5<\/h2>\n<\/p>\n<ul>\n<li><code>split()<\/code>: \u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5217\u8868\u3002<\/li>\n<li><code>join()<\/code>: \u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">s = &quot;Hello, World!&quot;<\/p>\n<p>parts = s.split(&quot;, &quot;)<\/p>\n<p>joined = &quot; - &quot;.join(parts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u5916\u90e8\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7279\u6b8a\u5b57\u7b26\u7684\u5904\u7406\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>7.1 HTML\u548cXML\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406HTML\u6216XML\u6570\u636e\u65f6\uff0c\u9700\u8981\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u4f8b\u5982<code>&lt;<\/code>\u548c<code>&gt;<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684html\u6a21\u5757\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import html<\/p>\n<p>escaped_html = html.escape(&quot;&lt;div&gt;Hello, World!&lt;\/div&gt;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>7.2 \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u67d0\u4e9b\u5b57\u7b26\u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u9700\u8981\u8fdb\u884c\u8f6c\u4e49\u3002\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = re.compile(r&quot;\\d+&quot;)<\/p>\n<p>matches = pattern.findall(&quot;There are 123 apples and 45 oranges.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u591a\u79cd\u5f15\u53f7\u7c7b\u578b\u3001\u591a\u884c\u5b57\u7b26\u4e32\u3001\u8f6c\u4e49\u5b57\u7b26\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u76f8\u5173\u7684\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5f15\u53f7\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u907f\u514d\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5904\u7406\u9519\u8bef\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u5355\u5f15\u53f7\uff08&#39;\uff09\u6216\u53cc\u5f15\u53f7\uff08&quot;\uff09\u5305\u88f9\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u5f15\u53f7\u901a\u5e38\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5355\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u793a\u4f8b\uff1a<code>str1 = &quot;It&#39;s a sunny day&quot;<\/code> \u6216 <code>str2 = &#39;He said &quot;Hello&quot;&#39;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5f15\u53f7\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e0e\u5b9a\u4e49\u5b57\u7b26\u4e32\u76f8\u540c\u7c7b\u578b\u7684\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u8fdb\u884c\u8f6c\u4e49\u3002\u4f8b\u5982\uff0c<code>str3 = &#39;It\\&#39;s a sunny day&#39;<\/code> \u6216 <code>str4 = &quot;He said \\&quot;Hello\\&quot;&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u5b89\u5168\u5730\u5305\u542b\u5f15\u53f7\u800c\u4e0d\u5f15\u53d1\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u7684\u573a\u666f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u7528\u4e8e\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5305\u542b\u6362\u884c\u7b26\u7684\u957f\u6587\u672c\u3002\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u591a\u884c\u5b57\u7b26\u4e32\u800c\u65e0\u9700\u4f7f\u7528\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;\u8fd9\u662f\u4e00\u4e2a\n\u591a\u884c\u5b57\u7b26\u4e32\n\u7684\u4f8b\u5b50\u3002&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\uff0c\u8fd8\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u81ea\u7531\u4f7f\u7528\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6253\u5f15\u53f7\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u3001\u4e09\u5f15\u53f7\u3002\u8fd9\u4e9b\u5f15\u53f7\u5728Python\u4e2d\u6709\u4e0d\u540c\u7684\u7528\u9014\uff0c\u4f8b\u5982\uff0c [&hellip;]","protected":false},"author":3,"featured_media":923303,"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\/923299"}],"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=923299"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923299\/revisions"}],"predecessor-version":[{"id":923304,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923299\/revisions\/923304"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/923303"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=923299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=923299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=923299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}