{"id":1147783,"date":"2025-01-13T16:29:15","date_gmt":"2025-01-13T08:29:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1147783.html"},"modified":"2025-01-13T16:29:18","modified_gmt":"2025-01-13T08:29:18","slug":"python%e5%a6%82%e4%bd%95%e9%99%8dstring%e5%8f%98%e6%88%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1147783.html","title":{"rendered":"Python\u5982\u4f55\u964dstring\u53d8\u6210"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25165934\/428b0fef-f4d0-4f05-8895-0ac3269144a2.webp\" alt=\"Python\u5982\u4f55\u964dstring\u53d8\u6210\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u6216\u683c\u5f0f<\/strong>\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>int()<\/code> \u6216 <code>float()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>split()<\/code>\u3001<code>replace()<\/code> \u7b49\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u62c6\u5206\u6216\u66ff\u6362\u5b57\u7b26\u4e32\u5185\u5bb9\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570 <code>int()<\/code> \u548c <code>float()<\/code> \u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>int()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>int()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u6574\u6570\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012\u7ed9 <code>int()<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>float()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>float()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u6d6e\u70b9\u6570\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012\u7ed9 <code>float()<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_str = &quot;123.45&quot;<\/p>\n<p>num_float = float(float_str)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>split()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5217\u8868\u3002<code>split()<\/code> \u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62c6\u5206\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u62c6\u5206\u7ed3\u679c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>split()<\/code> \u65b9\u6cd5\u7684\u9ed8\u8ba4\u5206\u9694\u7b26\u662f\u7a7a\u683c\u3002\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;apple,banana,cherry&quot;<\/p>\n<p>list_data = str_data.split(&#39;,&#39;)<\/p>\n<p>print(list_data)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p><code>split()<\/code> \u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u4ee5\u4fbf\u6839\u636e\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62c6\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;apple;banana;cherry&quot;<\/p>\n<p>list_data = str_data.split(&#39;;&#39;)<\/p>\n<p>print(list_data)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u5c06\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u683c\u5f0f\u4e3a <code>key1=value1;key2=value2<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u89e3\u6790\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>str_data = &quot;name=John;age=30;city=New York&quot;<\/p>\n<p>pattern = r&#39;(\\w+)=([^;]+)&#39;<\/p>\n<p>matches = re.findall(pattern, str_data)<\/p>\n<p>dict_data = dict(matches)<\/p>\n<p>print(dict_data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: &#39;30&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_dict(s, item_sep=&#39;;&#39;, key_value_sep=&#39;=&#39;):<\/p>\n<p>    items = s.split(item_sep)<\/p>\n<p>    dict_data = {}<\/p>\n<p>    for item in items:<\/p>\n<p>        key, value = item.split(key_value_sep)<\/p>\n<p>        dict_data[key] = value<\/p>\n<p>    return dict_data<\/p>\n<p>str_data = &quot;name=John;age=30;city=New York&quot;<\/p>\n<p>dict_data = str_to_dict(str_data)<\/p>\n<p>print(dict_data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: &#39;30&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684 <code>datetime<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002<code>datetime<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86 <code>strptime()<\/code> \u65b9\u6cd5\u6765\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>strptime()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>strptime()<\/code> \u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_str = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_format = &quot;%Y-%m-%d %H:%M:%S&quot;<\/p>\n<p>date_obj = datetime.strptime(date_str, date_format)<\/p>\n<p>print(date_obj)  # \u8f93\u51fa: 2023-10-25 14:30:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6620\u5c04\u6765\u5c06\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_bool(s):<\/p>\n<p>    if s.lower() in [&#39;true&#39;, &#39;yes&#39;, &#39;1&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    elif s.lower() in [&#39;false&#39;, &#39;no&#39;, &#39;0&#39;]:<\/p>\n<p>        return False<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{s}&#39; \u8f6c\u6362\u4e3a\u5e03\u5c14\u503c&quot;)<\/p>\n<p>print(str_to_bool(&quot;true&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(str_to_bool(&quot;False&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5143\u7ec4\u3001\u96c6\u5408\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>split()<\/code> \u65b9\u6cd5\u5e76\u7ed3\u5408 <code>tuple()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;apple,banana,cherry&quot;<\/p>\n<p>tuple_data = tuple(str_data.split(&#39;,&#39;))<\/p>\n<p>print(tuple_data)  # \u8f93\u51fa: (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>split()<\/code> \u65b9\u6cd5\u5e76\u7ed3\u5408 <code>set()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;apple,banana,cherry&quot;<\/p>\n<p>set_data = set(str_data.split(&#39;,&#39;))<\/p>\n<p>print(set_data)  # \u8f93\u51fa: {&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u548c\u7a7a\u767d\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\u548c\u7a7a\u767d\u5b57\u7b26\uff0c\u4f8b\u5982\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>replace()<\/code> \u65b9\u6cd5\u6765\u66ff\u6362\u8fd9\u4e9b\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>replace()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;Hello\\nWorld&quot;<\/p>\n<p>clean_str = str_data.replace(&quot;\\n&quot;, &quot; &quot;)<\/p>\n<p>print(clean_str)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>strip()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>strip()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;  Hello World  &quot;<\/p>\n<p>clean_str = str_data.strip()<\/p>\n<p>print(clean_str)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u573a\u666f\u4e2d\uff0c\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4f8b\u5982\u5d4c\u5957\u7684 JSON \u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u6216\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>json.loads()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>json.loads()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>dict_data = json.loads(json_str)<\/p>\n<p>print(dict_data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u548c\u65e0\u6548\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u8f6c\u6362\u65f6\uff0c\u9700\u8981\u8003\u8651\u7a7a\u5b57\u7b26\u4e32\u548c\u65e0\u6548\u8f93\u5165\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u7a7a\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;&quot;<\/p>\n<p>if not str_data:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e3a\u7a7a&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406\u65e0\u6548\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u5904\u7406\u65e0\u6548\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_data = &quot;abc&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(str_data)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{str_data}&#39; \u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u548c\u683c\u5f0f<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u8f6c\u6362\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u548c\u7279\u6b8a\u60c5\u51b5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u4e3a\u60a8\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1\u5b57\u7b26\u4e32\u8f6c\u6362\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>.lower()<\/code>\u65b9\u6cd5\u6765\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;Hello World&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_string.lower()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a<code>&quot;hello world&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5c0f\u5199\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\uff1f<\/strong><br \/>\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>.upper()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;Hello World&quot;<\/code>\uff0c\u6267\u884c<code>my_string.upper()<\/code>\u5c06\u8fd4\u56de<code>&quot;HELLO WORLD&quot;<\/code>\uff0c\u540c\u6837\u539f\u59cb\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>.capitalize()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u800c\u5176\u4f59\u5b57\u7b26\u5219\u4f1a\u53d8\u4e3a\u5c0f\u5199\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;hello world&quot;<\/code>\uff0c\u4f7f\u7528<code>my_string.capitalize()<\/code>\u5c06\u8fd4\u56de<code>&quot;Hello world&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u5904\u7406\u53e5\u5b50\u5f00\u5934\u7684\u5355\u8bcd\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u6216\u683c\u5f0f\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 in [&hellip;]","protected":false},"author":3,"featured_media":1147787,"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\/1147783"}],"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=1147783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147783\/revisions"}],"predecessor-version":[{"id":1147790,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147783\/revisions\/1147790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1147787"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1147783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1147783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1147783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}