{"id":2838,"date":"2025-06-01T17:24:52","date_gmt":"2025-06-04T09:37:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2838"},"modified":"2025-06-04T17:24:52","modified_gmt":"2025-06-04T09:37:51","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%9a%84str%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%9a%84str%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Python \u7684 str() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u61c9\u7528\u5be6\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, str() \u51fd\u6578, Python \u6559\u5b78, \u5b57\u7b26\u4e32\u8f49\u63db\"><\/p>\n<p>Python \u7684 str() \u51fd\u6578\u662f\u5c07\u6578\u503c\u6216\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u7684\u95dc\u9375\u5de5\u5177\uff0c\u5b83\u5728 Python \u7de8\u7a0b\u4e2d\u6975\u70ba\u5e38\u7528\u3002\u7121\u8ad6\u662f\u6578\u5b57\u3001\u5217\u8868\u9084\u662f\u5b57\u5178\uff0cstr() \u51fd\u6578\u90fd\u80fd\u8f15\u9b06\u5730\u5c07\u5b83\u5011\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u65b9\u4fbf\u5f8c\u7e8c\u8655\u7406\u3002<\/p>\n<h2>str() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>str() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c07\u4e00\u500b\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5e38\u898b\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\nnum = 123\nstr_num = str(num)\nprint(str_num)  # \u8f38\u51fa\uff1a'123'\n\n# \u5c07\u5217\u8868\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\nmy_list = [1, 2, 3]\nstr_list = str(my_list)\nprint(str_list)  # \u8f38\u51fa\uff1a'[1, 2, 3]'\n\n# \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\nmy_dict = {'name': 'John', 'age': 20}\nstr_dict = str(my_dict)\nprint(str_dict)  # \u8f38\u51fa\uff1a\"{'name': 'John', 'age': 20}\"\n<\/pre>\n<h2>str() \u51fd\u6578\u7684\u61c9\u7528<\/h2>\n<p>str() \u51fd\u6578\u53ef\u7528\u65bc\u5c07\u6578\u64da\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u65bc\u5b58\u5132\u6216\u50b3\u8f38\u3002\u4ee5\u4e0b\u662f\u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u4e26\u5b58\u5132\u5230\u6587\u4ef6\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5230\u6587\u4ef6\u4e2d\nmy_dict = {'name': 'John', 'age': 20}\nstr_dict = str(my_dict)\n\n# \u5c07\u5b57\u7b26\u4e32\u5beb\u5165\u6587\u4ef6\nwith open('data.txt', 'w') as f:\n    f.write(str_dict)\n\n# \u5c07\u5b57\u7b26\u4e32\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u53e6\u4e00\u500b\u51fd\u6578\ndef print_dict(dict_str):\n    dict_obj = eval(dict_str)  # \u4f7f\u7528 eval \u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5b57\u5178\n    print(dict_obj)\n\nprint_dict(str_dict)  # \u8f38\u51fa\uff1a{'name': 'John', 'age': 20}\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 str() \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u4f7f\u7528 eval() \u7684\u98a8\u96aa**\uff1a\u4f7f\u7528 eval() \u51fd\u6578\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5b57\u5178\u6642\uff0c\u9700\u5c0f\u5fc3\u8655\u7406\u4e0d\u53ef\u4fe1\u7684\u8f38\u5165\uff0c\u56e0\u70ba\u5b83\u53ef\u80fd\u57f7\u884c\u4e0d\u5b89\u5168\u7684\u4ee3\u78bc\u3002\u5efa\u8b70\u4f7f\u7528 json \u6a21\u7d44\u7684 `json.loads()` \u4f86\u5b89\u5168\u5730\u89e3\u6790\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"brush: python\">\n   import json\n\n   def safe_print_dict(dict_str):\n       dict_obj = json.loads(dict_str)  # \u4f7f\u7528 json.loads \u89e3\u6790\u5b57\u7b26\u4e32\n       print(dict_obj)\n   <\/pre>\n<p>2. **\u8f49\u63db\u7121\u6cd5\u8655\u7406\u7684\u5c0d\u8c61**\uff1a\u67d0\u4e9b\u5c0d\u8c61\uff08\u4f8b\u5982\u81ea\u5b9a\u7fa9\u985e\uff09\u53ef\u80fd\u7121\u6cd5\u76f4\u63a5\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u3002\u9700\u81ea\u5b9a\u7fa9 `__str__` \u65b9\u6cd5\u4f86\u63d0\u4f9b\u9069\u5408\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>str() \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u6578\u64da\u8f49\u63db\uff0c\u9084\u53ef\u4ee5\u5728\u6578\u64da\u8655\u7406\u548c\u65e5\u8a8c\u8a18\u9304\u4e2d\u767c\u63ee\u4f5c\u7528\u3002\u900f\u904e\u5c07\u8907\u96dc\u6578\u64da\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u9032\u884c\u65e5\u8a8c\u8a18\u9304\u548c\u6578\u64da\u50b3\u8f38\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5c0d Python \u7684\u5176\u4ed6\u9032\u968e\u7528\u6cd5\u611f\u8208\u8da3\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u7372\u53d6\u66f4\u591a\u6280\u5de7\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>str() \u51fd\u6578\u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u80fd\u5c07\u4efb\u4f55\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u70ba\u6578\u64da\u7684\u5b58\u5132\u8207\u50b3\u8f38\u63d0\u4f9b\u4fbf\u6377\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u638c\u63e1 str() \u51fd\u6578\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5728 Python \u958b\u767c\u4e2d\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1\uff1astr() \u51fd\u6578\u662f\u5426\u53ef\u4ee5\u8f49\u63db\u6240\u6709\u985e\u578b\u7684\u5c0d\u8c61\uff1f**<br \/>\n\u662f\u7684\uff0cstr() \u51fd\u6578\u53ef\u4ee5\u8f49\u63db\u5927\u591a\u6578\u5167\u5efa\u5c0d\u8c61\u985e\u578b\uff0c\u4f46\u5c0d\u65bc\u81ea\u5b9a\u7fa9\u985e\u578b\uff0c\u53ef\u80fd\u9700\u8981\u5be6\u73fe `__str__` \u65b9\u6cd5\u4ee5\u63d0\u4f9b\u6b63\u78ba\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<p>**Q2\uff1a\u4f7f\u7528 eval() \u6709\u4ec0\u9ebc\u98a8\u96aa\uff1f**<br \/>\neval() \u53ef\u4ee5\u57f7\u884c\u5b57\u7b26\u4e32\u4e2d\u7684 Python \u8868\u9054\u5f0f\uff0c\u82e5\u8f38\u5165\u672a\u7d93\u9a57\u8b49\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u5b89\u5168\u98a8\u96aa\u3002\u5efa\u8b70\u4f7f\u7528 json \u6a21\u7d44\u4f86\u907f\u514d\u9019\u985e\u554f\u984c\u3002<\/p>\n<p>**Q3\uff1a\u5982\u4f55\u5b89\u5168\u5730\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5b57\u5178\uff1f**<br \/>\n\u53ef\u4ee5\u4f7f\u7528 json.loads() \u65b9\u6cd5\u4f86\u5b89\u5168\u5730\u89e3\u6790\u5b57\u7b26\u4e32\uff0c\u5efa\u8b70\u907f\u514d\u4f7f\u7528 eval() \u4f86\u9632\u7bc4\u6f5b\u5728\u7684\u5b89\u5168\u554f\u984c\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u7684str()\u51fd\u6578\uff0c\u5c07\u6578\u503c\u6216\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u7a0b\u5e8f\u9700\u6c42\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0estr()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u60a8\u66f4\u6709\u6548\u5730\u5b8c\u6210\u60a8\u7684\u7a0b\u5e8f\u958b\u767c\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2838","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-JM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2838"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2838\/revisions"}],"predecessor-version":[{"id":2839,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2838\/revisions\/2839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}