{"id":1079350,"date":"2025-01-08T12:20:09","date_gmt":"2025-01-08T04:20:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079350.html"},"modified":"2025-01-08T12:20:11","modified_gmt":"2025-01-08T04:20:11","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%96%87%e6%9c%ac%e8%b5%8b%e5%80%bc%e7%bb%99%e5%8f%98%e9%87%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079350.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182424\/bd3f68e6-92de-4556-84f7-f306502e84c7.webp\" alt=\"python\u5982\u4f55\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u6765\u8fdb\u884c\u53d8\u91cf\u8d4b\u503c\uff0c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c\u5e76\u5c06\u6587\u672c\u5185\u5bb9\u8d4b\u503c\u7ed9\u8be5\u53d8\u91cf<\/strong>\uff0c\u4f8b\u5982\uff1a<code>variable_name = &quot;This is a string&quot;<\/code>, \u5bfc\u5165\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u3002<strong>\u5b9a\u4e49\u53d8\u91cf\u65f6\u9075\u5faa\u547d\u540d\u89c4\u5219<\/strong>\uff0c\u4f8b\u5982\u53d8\u91cf\u540d\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<strong>\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32<\/strong>\uff0c\u4f8b\u5982\uff1a<code>multi_line_string = &quot;&quot;&quot;This is a multi-line string&quot;&quot;&quot;<\/code>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u51e0\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u8d4b\u503c\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u662f\u7528\u4e8e\u5c06\u53f3\u8fb9\u7684\u503c\u8d4b\u7ed9\u5de6\u8fb9\u7684\u53d8\u91cf\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u6587\u672c\u5185\u5bb9\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable_name = &quot;This is a string&quot;<\/p>\n<p>another_variable = &#39;This is also a string&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>variable_name<\/code>\u548c<code>another_variable<\/code>\u5206\u522b\u88ab\u8d4b\u503c\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528\u53cc\u5f15\u53f7\u6216\u5355\u5f15\u53f7\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d8\u91cf\u547d\u540d\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u5728\u8d4b\u503c\u6587\u672c\u7ed9\u53d8\u91cf\u65f6\uff0c\u5fc5\u987b\u9075\u5faaPython\u7684\u53d8\u91cf\u547d\u540d\u89c4\u5219\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934<\/strong>\uff0c\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">valid_variable = &quot;Valid&quot;<\/p>\n<p>_valid_variable = &quot;Also valid&quot;<\/p>\n<p>1invalid_variable = &quot;Invalid&quot;  # \u8fd9\u5c06\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d8\u91cf\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable1 = &quot;Valid&quot;<\/p>\n<p>variable_name = &quot;Also valid&quot;<\/p>\n<p>variable-name = &quot;Invalid&quot;  # \u8fd9\u5c06\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable = &quot;lowercase&quot;<\/p>\n<p>Variable = &quot;Capitalized&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528Python\u5173\u952e\u5b57\u4f5c\u4e3a\u53d8\u91cf\u540d<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyword<\/p>\n<p>print(keyword.kwlist)  # \u5217\u51fa\u6240\u6709Python\u5173\u952e\u5b57<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08<code>&#39;&#39;&#39;<\/code> \u6216 <code>&quot;&quot;&quot;<\/code>\uff09\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;This is a multi-line string.<\/p>\n<p>It spans multiple lines.<\/p>\n<p>Each line is separated by a newline character.&quot;&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string_single = &#39;&#39;&#39;This is also a multi-line string.<\/p>\n<p>It works the same way.<\/p>\n<p>Each line is separated by a newline character.&#39;&#39;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u3001\u5f15\u53f7\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08<code><\/code>\uff09\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">escaped_string = &quot;This is a string with a newline character.\\nThis is the second line.&quot;<\/p>\n<p>quoted_string = &quot;He s<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>d, \\&quot;Python is awesome!\\&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u7b26\u4e32\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u8d4b\u503c\u5b57\u7b26\u4e32\u7ed9\u53d8\u91cf\u540e\uff0c\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u5207\u7247\u3001\u67e5\u627e\u548c\u66ff\u6362\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u8fde\u63a5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string1 = &quot;Hello&quot;<\/p>\n<p>string2 = &quot;World&quot;<\/p>\n<p>concatenated_string = string1 + &quot; &quot; + string2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u5207\u7247<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sliced_string = concatenated_string[0:5]  # \u7ed3\u679c\u4e3a &quot;Hello&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u627e\u5b50\u5b57\u7b26\u4e32<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">position = concatenated_string.find(&quot;World&quot;)  # \u7ed3\u679c\u4e3a 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u66ff\u6362<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">replaced_string = concatenated_string.replace(&quot;World&quot;, &quot;Python&quot;)  # \u7ed3\u679c\u4e3a &quot;Hello Python&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982<code>%<\/code>\u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code>\u65b9\u6cd5\u548cf-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528%\u64cd\u4f5c\u7b26<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>formatted_string = &quot;Hello, %s!&quot; % name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528str.format()\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 30<\/p>\n<p>formatted_string = &quot;I am {} years old.&quot;.format(age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">city = &quot;New York&quot;<\/p>\n<p>formatted_string = f&quot;I live in {city}.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>upper()<\/strong>\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">upper_string = &quot;hello&quot;.upper()  # \u7ed3\u679c\u4e3a &quot;HELLO&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>lower()<\/strong>\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lower_string = &quot;HELLO&quot;.lower()  # \u7ed3\u679c\u4e3a &quot;hello&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>strip()<\/strong>\uff1a\u79fb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stripped_string = &quot;  hello  &quot;.strip()  # \u7ed3\u679c\u4e3a &quot;hello&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>split()<\/strong>\uff1a\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">split_string = &quot;hello world&quot;.split()  # \u7ed3\u679c\u4e3a [&#39;hello&#39;, &#39;world&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>join()<\/strong>\uff1a\u5c06\u5b50\u5b57\u7b26\u4e32\u5217\u8868\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#39;hello&#39;, &#39;world&#39;]<\/p>\n<p>joined_string = &quot; &quot;.join(words)  # \u7ed3\u679c\u4e3a &quot;hello world&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u548c\u8f6c\u4e49\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5305\u542b\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26\uff08<code>\\n<\/code>\uff09\u3001\u5236\u8868\u7b26\uff08<code>\\t<\/code>\uff09\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">special_string = &quot;First line.\\nSecond line.&quot;<\/p>\n<p>tabbed_string = &quot;Item1\\tItem2\\tItem3&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u7b26\u4e32\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6216\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>number_string = str(number)  # \u7ed3\u679c\u4e3a &quot;123&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">integer_string = &quot;456&quot;<\/p>\n<p>integer_value = int(integer_string)  # \u7ed3\u679c\u4e3a 456<\/p>\n<p>float_string = &quot;123.45&quot;<\/p>\n<p>float_value = float(float_string)  # \u7ed3\u679c\u4e3a 123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u4e0e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u7f16\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p>encoded_string = original_string.encode(&#39;utf-8&#39;)  # \u7ed3\u679c\u4e3a b&#39;Hello, \\xe4\\xb8\\x96\\xe7\\x95\\x8c&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u89e3\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decoded_string = encoded_string.decode(&#39;utf-8&#39;)  # \u7ed3\u679c\u4e3a &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e00\u3001\u5b57\u7b26\u4e32\u6a21\u677f<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86<code>string<\/code>\u6a21\u5757\u4e2d\u7684<code>Template<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u6a21\u677f\u7684\u5b9a\u4e49\u548c\u66ff\u6362\u3002\u4f8b\u5982\uff1a<\/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>result = template.substitute(name=&quot;Alice&quot;)  # \u7ed3\u679c\u4e3a &quot;Hello, Alice!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5b57\u7b26\u4e32\u7684\u5e38\u89c1\u9519\u8bef\u4e0e\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u3001\u683c\u5f0f\u5316\u9519\u8bef\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u7d22\u5f15\u9519\u8bef<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    out_of_range = &quot;hello&quot;[10]<\/p>\n<p>except IndexError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)  # \u8f93\u51fa &quot;Error: string index out of range&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u9519\u8bef<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    formatted_string = &quot;My name is %s and I am %d years old.&quot; % (&quot;Alice&quot;, &quot;thirty&quot;)<\/p>\n<p>except TypeError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)  # \u8f93\u51fa &quot;Error: %d format: a number is required, not str&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e09\u3001\u5b57\u7b26\u4e32\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u9ad8\u7ea7\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u4f8b\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3001\u5b57\u7b26\u4e32\u53cd\u8f6c\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>matches = re.findall(pattern, text)  # \u7ed3\u679c\u4e3a [&#39;123&#39;, &#39;456&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u53cd\u8f6c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">reversed_string = &quot;hello&quot;[::-1]  # \u7ed3\u679c\u4e3a &quot;olleh&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u56db\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u540d<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u91cd\u590d\u4ee3\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>greeting1 = greet(&quot;Alice&quot;)<\/p>\n<p>greeting2 = greet(&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_int_conversion(s):<\/p>\n<p>    try:<\/p>\n<p>        return int(s)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>number = safe_int_conversion(&quot;123&quot;)<\/p>\n<p>invalid_number = safe_int_conversion(&quot;abc&quot;)  # \u7ed3\u679c\u4e3a None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5e76\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u90fd\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5b9e\u73b0\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u5e76\u8d4b\u503c\u6587\u672c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u6765\u5b9a\u4e49\u53d8\u91cf\u5e76\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u5b83\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<code>my_variable = &quot;\u8fd9\u662f\u4e00\u6bb5\u6587\u672c&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u53d8\u91cf<code>my_variable<\/code>\u4e2d\uff0c\u60a8\u53ef\u4ee5\u968f\u65f6\u5f15\u7528\u6216\u4fee\u6539\u8be5\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p><strong>Python\u4e2d\u5b57\u7b26\u4e32\u7684\u5e38\u89c1\u683c\u5f0f\u5316\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>name = &quot;Alice&quot;; greeting = f&quot;\u4f60\u597d\uff0c{name}\uff01&quot;<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u6216\u8005\u767e\u5206\u53f7\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u8fd9\u4e9b\u65b9\u5f0f\u4f7f\u5f97\u6587\u672c\u8d4b\u503c\u66f4\u52a0\u7075\u6d3b\u548c\u52a8\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u591a\u884c\u6587\u672c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5c06\u591a\u884c\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08<code>&#39;&#39;&#39;<\/code>\u6216<code>&quot;&quot;&quot;<\/code>\uff09\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>text = &quot;&quot;&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6587\u672c\u3002 \u8fd9\u662f\u7b2c\u4e8c\u884c\u6587\u672c\u3002&quot;&quot;&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u8ba9\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4fdd\u7559\u6587\u672c\u4e2d\u7684\u6362\u884c\u7b26\u548c\u7a7a\u683c\uff0c\u786e\u4fdd\u6587\u672c\u7684\u683c\u5f0f\u4e0d\u4f1a\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5c06\u6587\u672c\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u6765\u8fdb\u884c\u53d8\u91cf\u8d4b\u503c\uff0c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c\u5e76\u5c06\u6587\u672c\u5185\u5bb9\u8d4b\u503c\u7ed9\u8be5 [&hellip;]","protected":false},"author":3,"featured_media":1079360,"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\/1079350"}],"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=1079350"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079350\/revisions"}],"predecessor-version":[{"id":1079363,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079350\/revisions\/1079363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079360"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}