{"id":545,"date":"2024-12-26T22:41:10","date_gmt":"2024-12-26T14:41:10","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=545"},"modified":"2024-12-26T22:41:10","modified_gmt":"2024-12-26T14:41:10","slug":"%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90-python-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c%ef%bc%9a%e6%96%b9%e6%b3%95%e4%b8%8e%e6%8a%80%e5%b7%a7%e5%a4%a7%e5%85%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/26\/%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90-python-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c%ef%bc%9a%e6%96%b9%e6%b3%95%e4%b8%8e%e6%8a%80%e5%b7%a7%e5%a4%a7%e5%85%a8\/","title":{"rendered":"\u5168\u9762\u89e3\u6790 Python \u5b57\u7b26\u4e32\u64cd\u4f5c\uff1a\u65b9\u6cd5\u4e0e\u6280\u5de7\u5927\u5168"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5b57\u7b26\u4e32\u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u6587\u672c\u3002\u4ee5\u4e0b\u5c06\u4ecb\u7ecd\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5b57\u7b26\u4e32\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Hello, Python!\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(text&#91;0])  # \u8f93\u51fa\uff1aH\nprint(text&#91;-1])  # \u8f93\u51fa\uff1a!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u62fc\u63a5\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>greeting = \"Hello\"\nname = \"Alice\"\nprint(greeting + \", \" + name + \"!\")  # \u8f93\u51fa\uff1aHello, Alice!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u91cd\u590d\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Python! \" * 3)  # \u8f93\u51fa\uff1aPython! Python! Python! <\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5e38\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u6539\u53d8\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5199<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Hello, Python!\"\nprint(text.lower())  # \u8f6c\u5c0f\u5199\uff1ahello, python!\nprint(text.upper())  # \u8f6c\u5927\u5199\uff1aHELLO, PYTHON!\nprint(text.capitalize())  # \u9996\u5b57\u6bcd\u5927\u5199\uff1aHello, python!\nprint(text.title())  # \u6bcf\u4e2a\u5355\u8bcd\u9996\u5b57\u6bcd\u5927\u5199\uff1aHello, Python!\nprint(text.swapcase())  # \u5927\u5c0f\u5199\u4e92\u6362\uff1ahELLO, pYTHON!<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u5b57\u7b26\u4e32\u641c\u7d22<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python is awesome!\"\n\n# \u67e5\u627e\u5b50\u5b57\u7b26\u4e32\nprint(text.find(\"is\"))  # \u8fd4\u56de\u7d22\u5f15\uff1a7\nprint(text.rfind(\"is\"))  # \u4ece\u53f3\u4fa7\u67e5\u627e\uff1a7\nprint(text.index(\"is\"))  # \u8fd4\u56de\u7d22\u5f15\uff1a7\uff08\u627e\u4e0d\u5230\u4f1a\u62a5\u9519\uff09\n\n# \u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\nprint(\"is\" in text)  # \u8f93\u51fa\uff1aTrue\nprint(\"hello\" not in text)  # \u8f93\u51fa\uff1aTrue<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 \u5b57\u7b26\u4e32\u66ff\u6362<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"I love Java\"\nprint(text.replace(\"Java\", \"Python\"))  # \u66ff\u6362\u4e3a\uff1aI love Python<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.4 \u5220\u9664\u7a7a\u683c\u6216\u7279\u5b9a\u5b57\u7b26<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"   Python   \"\nprint(text.strip())  # \u5220\u9664\u4e24\u7aef\u7a7a\u683c\uff1aPython\nprint(text.lstrip())  # \u5220\u9664\u5de6\u4fa7\u7a7a\u683c\uff1aPython   \nprint(text.rstrip())  # \u5220\u9664\u53f3\u4fa7\u7a7a\u683c\uff1a   Python<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.5 \u5206\u5272\u4e0e\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python,Java,C++\"\n\n# \u5206\u5272\u5b57\u7b26\u4e32\nlanguages = text.split(\",\")\nprint(languages)  # \u8f93\u51fa\uff1a&#91;'Python', 'Java', 'C++']\n\n# \u4f7f\u7528\u7279\u5b9a\u5206\u9694\u7b26\u8fde\u63a5\u5b57\u7b26\u4e32\nprint(\" | \".join(languages))  # \u8f93\u51fa\uff1aPython | Java | C++<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.6 \u68c0\u67e5\u5b57\u7b26\u4e32\u7279\u6027<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python3\"\n\nprint(text.isalpha())  # \u68c0\u67e5\u662f\u5426\u5168\u662f\u5b57\u6bcd\uff1aFalse\nprint(text.isdigit())  # \u68c0\u67e5\u662f\u5426\u5168\u662f\u6570\u5b57\uff1aFalse\nprint(text.isalnum())  # \u68c0\u67e5\u662f\u5426\u662f\u5b57\u6bcd\u548c\u6570\u5b57\uff1aTrue\nprint(text.isspace())  # \u68c0\u67e5\u662f\u5426\u5168\u662f\u7a7a\u767d\u5b57\u7b26\uff1aFalse\nprint(\"PYTHON\".isupper())  # \u662f\u5426\u5168\u5927\u5199\uff1aTrue\nprint(\"python\".islower())  # \u662f\u5426\u5168\u5c0f\u5199\uff1aTrue<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.7 \u5b57\u7b26\u4e32\u5bf9\u9f50<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python\"\n\nprint(text.center(10, \"*\"))  # \u5c45\u4e2d\u5bf9\u9f50\uff1a**Python**\nprint(text.ljust(10, \"-\"))  # \u5de6\u5bf9\u9f50\uff1aPython----\nprint(text.rjust(10, \"-\"))  # \u53f3\u5bf9\u9f50\uff1a----Python<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.1 \u4f7f\u7528 <code>%<\/code> \u683c\u5f0f\u5316<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\nage = 25\nprint(\"Name: %s, Age: %d\" % (name, age))  # \u8f93\u51fa\uff1aName: Alice, Age: 25<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2 \u4f7f\u7528 <code>str.format<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Name: {}, Age: {}\".format(name, age))  # \u8f93\u51fa\uff1aName: Alice, Age: 25\nprint(\"Name: {0}, Age: {1}\".format(name, age))  # \u4f7f\u7528\u7d22\u5f15<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.3 \u4f7f\u7528 f-string\uff08\u63a8\u8350\uff09<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(f\"Name: {name}, Age: {age}\")  # \u8f93\u51fa\uff1aName: Alice, Age: 25<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e0e\u89e3\u7801<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u7801\u4e3a\u5b57\u8282<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python\"\nencoded_text = text.encode(\"utf-8\")\nprint(encoded_text)  # \u8f93\u51fa\uff1ab'Python'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>decoded_text = encoded_text.decode(\"utf-8\")\nprint(decoded_text)  # \u8f93\u51fa\uff1aPython<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u5b57\u7b26\u4e32\u4e0d\u53ef\u53d8\u6027<\/strong><\/h3>\n\n\n\n<p>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u6bcf\u6b21\u4fee\u6539\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Hello\"\ntext = text + \" World\"  # \u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u5bf9\u8c61\nprint(text)  # \u8f93\u51fa\uff1aHello World<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002\u4ece\u57fa\u672c\u64cd\u4f5c\u5230\u9ad8\u7ea7\u529f\u80fd\u7684\u638c\u63e1\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5904\u7406\u6587\u672c\u6570\u636e\u3002\u591a\u5b9e\u8df5\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f1a\u8ba9\u4f60\u5bf9 Python \u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u7b26\u4e32\u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u4eec\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u6587\u672c\u3002\u4ee5\u4e0b\u5c06\u4ecb\u7ecd\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=545"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/545\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}