{"id":1031440,"date":"2024-12-31T11:25:31","date_gmt":"2024-12-31T03:25:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1031440.html"},"modified":"2024-12-31T11:25:33","modified_gmt":"2024-12-31T03:25:33","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%b5%8b%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1031440.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u8d4b\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6bc89019-b83b-400d-b1f0-42042acfdd88.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u8d4b\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>Python\u7ed9\u5b57\u7b26\u4e32\u8d4b\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528f-string\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\u5177\u4f53\u89e3\u91ca\u5982\u4e0b\uff1aPython\u4e2d\uff0c\u7ed9\u5b57\u7b26\u4e32\u8d4b\u53d8\u91cf\u662f\u4e00\u79cd\u57fa\u672c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08<code>=<\/code>\uff09\uff0c\u5c06\u5b57\u7b26\u4e32\u76f4\u63a5\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u6765\u8d4b\u503c\u53d8\u91cf\uff0c\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u5305\u62ec\uff1a\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001<code>str.format()<\/code>\u65b9\u6cd5\u4ee5\u53caf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8d4b\u503c\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u8d4b\u503c\u8fd0\u7b97\u7b26\u662f\u6700\u57fa\u672c\u3001\u6700\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u8d4b\u503c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = &quot;Hello, &quot; + name + &quot;!&quot;<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>&quot;Alice&quot;<\/code> \u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf <code>name<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 <code>+<\/code> \u5c06 <code>name<\/code> \u548c\u5176\u4ed6\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6700\u7ec8\u8d4b\u503c\u7ed9 <code>greeting<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u8f83\u65e9\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f7f\u7528\u767e\u5206\u53f7 <code>%<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\u6765\u63d2\u5165\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 25<\/p>\n<p>info = &quot;Name: %s, Age: %d&quot; % (name, age)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>%s<\/code> \u662f\u5b57\u7b26\u4e32\u5360\u4f4d\u7b26\uff0c<code>%d<\/code> \u662f\u6574\u6570\u5360\u4f4d\u7b26\uff0c\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code> \u88ab\u63d2\u5165\u5230\u76f8\u5e94\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001str.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.format()<\/code> \u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u73b0\u4ee3\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\u6765\u63d2\u5165\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 30<\/p>\n<p>info = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5927\u62ec\u53f7 <code>{}<\/code> \u662f\u5360\u4f4d\u7b26\uff0c<code>format()<\/code> \u65b9\u6cd5\u4f1a\u5c06\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code> \u63d2\u5165\u5230\u76f8\u5e94\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h3>\n<\/p>\n<p><p>f-string \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd <code>f<\/code>\uff0c\u5e76\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u76f4\u63a5\u63d2\u5165\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;David&quot;<\/p>\n<p>age = 35<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code> \u88ab\u76f4\u63a5\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u62ec\u53f7 <code>{}<\/code> \u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>string<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86 <code>Template<\/code> \u7c7b\uff0c\u7528\u4e8e\u66ff\u6362\u6a21\u677f\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>name = &quot;Eve&quot;<\/p>\n<p>age = 40<\/p>\n<p>template = Template(&quot;Name: $name, Age: $age&quot;)<\/p>\n<p>info = template.substitute(name=name, age=age)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>$name<\/code> \u548c <code>$age<\/code> \u662f\u5360\u4f4d\u7b26\uff0c<code>substitute<\/code> \u65b9\u6cd5\u4f1a\u5c06\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code> \u63d2\u5165\u5230\u76f8\u5e94\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u6765\u7ed9\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet():<\/p>\n<p>    name = &quot;Frank&quot;  # \u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>    return greeting<\/p>\n<p>name = &quot;Grace&quot;  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>greeting = f&quot;Hi, {name}!&quot;<\/p>\n<p>print(greet())<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>name<\/code> \u53d8\u91cf\u5728\u51fd\u6570 <code>greet<\/code> \u4e2d\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u51fd\u6570\u5916\u90e8\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5206\u522b\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u6765\u7ed9\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868<\/p>\n<p>names = [&quot;Hank&quot;, &quot;Ivy&quot;, &quot;Jack&quot;]<\/p>\n<p>greeting = f&quot;Hello, {names[0]}!&quot;<\/p>\n<p>print(greeting)<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178<\/strong><\/h2>\n<p>person = {&quot;name&quot;: &quot;Kate&quot;, &quot;age&quot;: 45}<\/p>\n<p>info = f&quot;Name: {person[&#39;name&#39;]}, Age: {person[&#39;age&#39;]}&quot;<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u6216\u952e\u6765\u8bbf\u95ee\u5e76\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u5408\u51fd\u6570\u548c\u7c7b\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u51fd\u6570\u548c\u7c7b\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u51fd\u6570<\/p>\n<p>def get_greeting(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>name = &quot;Liam&quot;<\/p>\n<p>greeting = get_greeting(name)<\/p>\n<p>print(greeting)<\/p>\n<h2><strong>\u4f7f\u7528\u7c7b<\/strong><\/h2>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def get_info(self):<\/p>\n<p>        return f&quot;Name: {self.name}, Age: {self.age}&quot;<\/p>\n<p>person = Person(&quot;Mia&quot;, 50)<\/p>\n<p>info = person.get_info()<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u51fd\u6570\u548c\u7c7b\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed3\u5408\u6587\u4ef6\u548c\u6570\u636e\u5e93\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u8d4b\u503c\u5e38\u5e38\u9700\u8981\u4ece\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/p>\n<p>with open(&quot;data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    name = file.readline().strip()<\/p>\n<p>greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>print(greeting)<\/p>\n<h2><strong>\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>import sqlite3<\/p>\n<p>conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT name, age FROM users WHERE id = 1&quot;)<\/p>\n<p>row = cursor.fetchone()<\/p>\n<p>name, age = row<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(info)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4ece\u6587\u4ef6\u548c\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5904\u7406\u548c\u8d4b\u503c\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Name: Olivia, Age: 55&quot;<\/p>\n<p>match = re.search(r&quot;Name: (\\w+), Age: (\\d+)&quot;, text)<\/p>\n<p>if match:<\/p>\n<p>    name, age = match.groups()<\/p>\n<p>    info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>    print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u636e\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5e76\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u7ed3\u5408\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668<\/p>\n<p>def name_generator():<\/p>\n<p>    yield &quot;Paul&quot;<\/p>\n<p>    yield &quot;Quincy&quot;<\/p>\n<p>    yield &quot;Rita&quot;<\/p>\n<p>for name in name_generator():<\/p>\n<p>    greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<h2><strong>\u4f7f\u7528\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>names = [&quot;Sam&quot;, &quot;Tina&quot;, &quot;Uma&quot;]<\/p>\n<p>name_iterator = iter(names)<\/p>\n<p>for name in name_iterator:<\/p>\n<p>    greeting = f&quot;Hi, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u6765\u751f\u6210\u548c\u8fed\u4ee3\u5b57\u7b26\u4e32\u53d8\u91cf\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u7ed3\u5408\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u5404\u79cd\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u6808<\/p>\n<p>stack = []<\/p>\n<p>stack.append(&quot;Vera&quot;)<\/p>\n<p>stack.append(&quot;Will&quot;)<\/p>\n<p>stack.append(&quot;Xander&quot;)<\/p>\n<p>while stack:<\/p>\n<p>    name = stack.pop()<\/p>\n<p>    greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<h2><strong>\u4f7f\u7528\u961f\u5217<\/strong><\/h2>\n<p>from collections import deque<\/p>\n<p>queue = deque([&quot;Yvonne&quot;, &quot;Zane&quot;, &quot;Adam&quot;])<\/p>\n<p>while queue:<\/p>\n<p>    name = queue.popleft()<\/p>\n<p>    greeting = f&quot;Hi, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u6808\u548c\u961f\u5217\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u53d8\u91cf\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u7ed3\u5408\u7f51\u7edc\u8bf7\u6c42\u548cAPI\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u8d4b\u503c\u5e38\u5e38\u9700\u8981\u4ece\u7f51\u7edc\u8bf7\u6c42\u548cAPI\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u4eceAPI\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/api.example.com\/user\/1&quot;)<\/p>\n<p>data = response.json()<\/p>\n<p>name = data[&quot;name&quot;]<\/p>\n<p>age = data[&quot;age&quot;]<\/p>\n<p>info = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(info)<\/p>\n<h2><strong>\u4ece\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<p>text = response.text<\/p>\n<p>match = re.search(r&quot;&lt;title&gt;(.*?)&lt;\/title&gt;&quot;, text)<\/p>\n<p>if match:<\/p>\n<p>    title = match.group(1)<\/p>\n<p>    print(f&quot;Title: {title}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4eceAPI\u548c\u7f51\u7edc\u8bf7\u6c42\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u8d4b\u503c\u7ed9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u7ed3\u5408\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import asyncio<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b<\/strong><\/h2>\n<p>def thread_function(name):<\/p>\n<p>    greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<p>thread = threading.Thread(target=thread_function, args=(&quot;Bella&quot;,))<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<h2><strong>\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/strong><\/h2>\n<p>async def async_function(name):<\/p>\n<p>    greeting = f&quot;Hi, {name}!&quot;<\/p>\n<p>    print(greeting)<\/p>\n<p>asyncio.run(async_function(&quot;Carter&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u9ad8\u6548\u548c\u54cd\u5e94\u8fc5\u901f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u7ed3\u5408\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u8bbe\u8ba1\u6a21\u5f0f\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u8bbe\u8ba1\u6a21\u5f0f\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/p>\n<p>class Greeting:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def get_greeting(self):<\/p>\n<p>        return f&quot;Hello, {self.name}!&quot;<\/p>\n<p>greet = Greeting(&quot;Diana&quot;)<\/p>\n<p>print(greet.get_greeting())<\/p>\n<h2><strong>\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff08\u5355\u4f8b\u6a21\u5f0f\uff09<\/strong><\/h2>\n<p>class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    def __new__(cls, *args, kwargs):<\/p>\n<p>        if not cls._instance:<\/p>\n<p>            cls._instance = super().__new__(cls, *args, kwargs)<\/p>\n<p>        return cls._instance<\/p>\n<p>singleton1 = Singleton()<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p>print(singleton1 is singleton2)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u8bbe\u8ba1\u6a21\u5f0f\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7ed3\u6784\u5316\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 Python \u4e2d\u7075\u6d3b\u5730\u7ed9\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u6ee1\u8db3\u5404\u79cd\u4e0d\u540c\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u8fd8\u662f\u590d\u6742\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u8bbe\u8ba1\u6a21\u5f0f\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8d4b\u503c\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u548c\u89e3\u91ca\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u5c06\u5b57\u7b26\u4e32\u8d4b\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_string = &quot;Hello, World!&quot;<\/code>\u3002\u8fd9\u5c06\u628a\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\u5b58\u50a8\u5728\u53d8\u91cf<code>my_string<\/code>\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5b57\u7b26\u4e32\u53d8\u91cf\uff1f<\/strong><br \/>\u4e00\u65e6\u5b57\u7b26\u4e32\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u540d\u8bbf\u95ee\u548c\u64cd\u4f5c\u5b83\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6253\u5370\u53d8\u91cf\u7684\u5185\u5bb9\uff1a<code>print(my_string)<\/code>\uff0c\u6216\u8005\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62fc\u63a5\u3001\u5207\u7247\u7b49\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5982<code>my_string.upper()<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5c06\u591a\u884c\u6587\u672c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08<code>&#39;&#39;&#39;<\/code>\u6216<code>&quot;&quot;&quot;<\/code>\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u3002\n\u8fd9\u662f\u7b2c\u4e8c\u884c\u3002\n\u8fd9\u662f\u7b2c\u4e09\u884c\u3002&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\u5b58\u50a8\u591a\u884c\u6587\u672c\uff0c\u5e76\u5728\u9700\u8981\u65f6\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed9\u5b57\u7b26\u4e32\u8d4b\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528f-string\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u51fd\u6570\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1031450,"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\/1031440"}],"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=1031440"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1031440\/revisions"}],"predecessor-version":[{"id":1031454,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1031440\/revisions\/1031454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1031450"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1031440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1031440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1031440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}