{"id":397276,"date":"2021-03-20T21:31:33","date_gmt":"2021-03-20T13:31:33","guid":{"rendered":"http:\/\/4563.org\/?p=397276"},"modified":"2021-03-20T21:31:33","modified_gmt":"2021-03-20T13:31:33","slug":"print-%e7%9a%84-string-%e5%86%85%e5%90%ab%e6%9c%89%e4%b8%ad%e6%96%87%ef%bc%8c%e5%a6%82%e6%9e%9c%e4%b8%ba%e5%a5%97%e4%b8%8a%e4%ba%86%e9%a2%9c%e8%89%b2%e5%ae%9a%e4%b9%89%e5%89%8d%e7%bc%80%ef%bc%8c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=397276","title":{"rendered":"print \u7684 string \u5185\u542b\u6709\u4e2d\u6587\uff0c\u5982\u679c\u4e3a\u5957\u4e0a\u4e86\u989c\u8272\u5b9a\u4e49\u524d\u7f00\uff0c\u90a3\u4e48 ljust\uff0c rjust \u7684\u4f4d\u7f6e\u63a7\u5236\uff0c\u57fa\u672c\u4e0a\u662f\u5e9f\u4e86\uff1f \u5982\u4f55\u7834?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  print \u7684 string \u5185\u542b\u6709\u4e2d\u6587\uff0c\u5982\u679c\u4e3a\u5957\u4e0a\u4e86\u989c\u8272\u5b9a\u4e49\u524d\u7f00\uff0c\u90a3\u4e48 ljust\uff0c rjust \u7684\u4f4d\u7f6e\u63a7\u5236\uff0c\u57fa\u672c\u4e0a\u662f\u5e9f\u4e86\uff1f \u5982\u4f55\u7834?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qazwsxkevin <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982:<\/p>\n<pre><code>AStr = \"Test\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u82f1\u6df7\u5408\u7684\u5b57\u53e5.\" BStr = \"\u000033[31m\" + AStr  + \"\u000033[0m\" # \u4f7f AStr \u53d8\u6210\u7ea2\u8272\u5b57\u4f53  print(len(AStr)) print(len(BStr))  # \u8fd9\u91cc\u7684\u957f\u5ea6\u8f93\u51fa\u5b8c\u5168\u662f\u4e24\u56de\u4e8b  # \u90a3\u4e48\u5982\u679c\u6709\u8fd9\u6837\u4e00\u4e2a string def colorRed(s)     retrun \"\u000033[31m\" + s  + \"\u000033[0m\"  assembleStr =  colorRed(\"Test\") + \",\" + colorBlue(\"\u8fd9\u662f\u4e00\u4e2a\") + colorYellow(\"\u4e2d\u82f1\u6df7\u5408\u7684\u5b57\u53e5.\")  ...... ... .  print(xxx.ljust(20),       assembleStr.ljust(80),       yyy.jlust(30))        assembleStr \u7684 ljust \u6839\u672c\u4e0d\u53ef\u7cbe\u51c6\u63a7\u5236\u4f4d\u7f6e\uff0c\u591a\u884c\u5185\u5bb9\u957f\u77ed\u4e0d\u4e00\u5185\u5bb9\u7684\u8bdd\uff0c\u6574\u4f53\u5c4f\u5e55\u4e0a\u628a\u540e\u9762 yyy \u7684\u4f4d\u7f6e\u5f04\u5f97\u4e0d\u6574\u9f50 ljust\uff0crjust \u5728\u591a\u989c\u8272\u6df7\u5408\u7ec4\u88c5 string \u7684\u60c5\u51b5\u4e0b,\u57fa\u672c\u4e0a\u662f\u5e9f\u4e86...  <\/code><\/pre>\n<p>\u60f3\u5bfb\u6c42 2 \u79cd\u89e3\u51b3\u65b9\u6cd5: <\/p>\n<p>1 \u3001\u66f4\u8fdb\u4e00\u6b65\u5982\u4f55\u7cbe\u51c6\u63a7\u5236 jljust \u7684\u7a8d\u95e8?<br \/> 2 \u3001\u662f\u5426\u6709\u8fd9\u79cd\u9002\u5408\u591a\u989c\u8272\u6df7\u5408 print\uff0c\u800c\u4e14\u80fd\u7cbe\u51c6\u63a7\u5236\u957f\u5ea6\u7684\u8f6e\u5b50?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5543065\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BrettD <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u4ee5 UTF-8 \u7f16\u7801\u50a8\u5b58\u5728\u5b57\u7b26\u4e32\u91cc\u7684\u8bdd\uff0c\u622a\u53d6\u5b50\u5b57\u7b26\u4e32\u5e94\u8be5\u6309\u7167 UTF-8 \u7f16\u7801\u89c4\u5219\u8ba1\u7b97\u4e0b\u8868\u5982                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543066\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BrettD <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u4ee5 UTF-8 \u7f16\u7801\u50a8\u5b58\u5728\u5b57\u7b26\u4e32\u91cc\u7684\u8bdd\uff0c\u622a\u53d6\u5b50\u5b57\u7b26\u4e32\u5e94\u8be5\u6309\u7167 UTF-8 \u7f16\u7801\u89c4\u5219\u8ba1\u7b97\u4e0b\u6807\u4f4d\u7f6e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543067\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/willmcgugan\/rich                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543068\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : qazwsxkevin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BrettD \u672a\u80fd\u660e\u767d\u539f\u7406\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543069\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : qazwsxkevin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii \u7b80\u5355\u8bd5\u4e86\u4e00\u4e0b\uff0c\u4ee5\u5b83\u5b98\u7f51\u7684\u4f8b\u5b50 print \u90fd\u4e0d\u80fd\u6b63\u786e\u663e\u793a\u51fa\u5b57\u4f53\u989c\u8272\u2026\u2026PyCharm\uff0c3.66 \u73af\u5883\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543070\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oott123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5148\u8865\u767d \u518d\u7740\u8272                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543071\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qazwsxkevin #5 Win10 CMD \u548c Microsoft Terminal \u90fd\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u989c\u8272\u3002\u5982\u679c\u4f60\u7684\u73af\u5883\u4e0d\u80fd\u6b63\u786e\u663e\u793a\u989c\u8272\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u662f\u4e0d\u662f\u4f7f\u7528\u4e86 PyCharm \u81ea\u5e26\u7684\u7ec8\u7aef\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543072\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : qazwsxkevin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @oott123 \u611f\u8c22\u60a8\u7684\u89e3\u7b54\uff0c\u6211\u5728\u5176\u4ed6\u8d44\u6599\u4e86\u89e3\u5230 pycharm \u7684\u63a7\u5236\u53f0\u754c\u9762\u4e0d\u652f\u6301\u9ed8\u8ba4\u989c\u8272\u7a7a\u95f4\uff0c\u90a3\u4e48\u5982\u679c\u6211\u8981\u5728 pycharm \u8c03\u8bd5 rich\uff0c\u90a3\u5c31\u662f\u5f88\u56f0\u96be\u7684\u4e8b\u4e86\u5427\uff0c\u8fd8\u6709\u60a8\u63d0\u5230\u7684\u8865\u767d\u662f\u600e\u4e48\u7406\u89e3\u5462\uff1f\u62b1\u6b49\uff0c\u95ee\u5f97\u90a3\u4e48\u7f57\u55e6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543073\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : qazwsxkevin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii \u7684\u786e\u662f\u7684\uff0cConsole \u663e\u793a\u989c\u8272\u6d41\u6c34\u5b57\u7b26\u5f88\u5fc5\u987b\uff0c\u800c\u6211\u4e00\u76f4\u5728\u7528 pycharm \u6765\u8c03\u8bd5\u7a0b\u5e8f\uff0c\u8981\u6362 IDE \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543074\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qazwsxkevin #9 \u5982\u679c\u662f Windows \u73af\u5883\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528\u4e00\u4e0b\u5de8\u786c\u51fa\u54c1\u7684 Terminal\uff0chttps:\/\/github.com\/microsoft\/terminal \u3002<\/p>\n<p>\u5982\u679c\u662f macOS \u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528 iTerm2 \u3002\u5982\u679c\u662f Linux \u7684\u8bdd\uff0c\u81ea\u5e26\u7ec8\u7aef\u901a\u5e38\u5c31\u53ef\u4ee5                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543075\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qazwsxkevin #9 IDE \u53ea\u662f\u65b9\u4fbf\u4f60\u5199\u4ee3\u7801\u548c\u8c03\u8bd5\u800c\u5df2\uff0c\u4e0d\u8981\u88ab\u5b83\u7ea6\u675f\u4f4f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543076\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oott123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qazwsxkevin \u8865\u767d\u5c31\u662f\u8bf4\u4f60\u7528 ljust \/ rjust \u8865\u7a7a\u683c\u8c03\u6574\u4f4d\u7f6e\u7684\u610f\u601d\uff0c\u5982\u679c\u662f\u56e0\u4e3a\u989c\u8272\u63a7\u5236\u5b57\u7b26\u5f71\u54cd\u4e86\u7a7a\u767d\u6570\u91cf\uff0c\u90a3\u4e48\u5148\u8c03\u6574\u4f4d\u7f6e\u518d\u52a0\u989c\u8272\u5c31\u53ef\u4ee5\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543077\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AndyAO <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qazwsxkevin #9 \u53ef\u4ee5\u5c06\u5185\u7f6e\u7ec8\u7aef\u66f4\u6362\u4e3a pwsh\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4 CMD\uff0c\u6ca1\u8bb0\u9519\u7684\u8bdd\uff0c\u90a3\u91cc\u662f\u652f\u6301 ANSI \u8272\u5f69\u7684\uff0c\u4e0d\u8fc7\u4f7f\u7528\u4e60\u60ef\u9700\u8981\u6539\u6539                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5543078\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6392\u7248\u7684\u8bdd tabulate[widechars] \u5427\uff0c\u9ed8\u8ba4\u5c31\u652f\u6301 ANSI \u63a7\u5236\u5b57\u7b26\uff0c\u6dfb\u52a0 widechars \u652f\u6301\u53cc\u5bbd\u5b57\u7b26\u3002<\/p>\n<p>colorama \u53ef\u4ee5\u5728 CMD \u91cc\u4f7f\u7528 ANSI \u989c\u8272\u63a7\u5236\uff0c\u65b9\u5f0f\u662f\u6302\u63a5 sys.stdout \u548c sys.stderr\uff0c\u5e76\u5c06\u63a7\u5236\u5b57\u7b26\u4e32\u7ffb\u8bd1\u4e3a win32 API call                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>print \u7684 string \u5185\u542b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/397276"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=397276"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/397276\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397276"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}