{"id":205,"date":"2023-02-22T22:22:15","date_gmt":"2023-02-22T14:22:15","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=205"},"modified":"2023-04-30T15:29:06","modified_gmt":"2023-04-30T07:29:06","slug":"python-string-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-string-operations\/","title":{"rendered":"Python\u5b57\u7b26\u4e32\u64cd\u4f5c"},"content":{"rendered":"<h2>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf<\/h2>\n<h3>\u53cc\u5f15\u53f7<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam = &quot;This is Tom&#039; cat&quot;\n>&gt;&gt; spam\n&quot;This is Tom&#039; cat&quot;<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; print(&#039;\\&#039;,\\&quot;,\\t,\\n,\\\\&#039;)\n&#039;,&quot;,    ,\n,\\<\/code><\/pre>\n<h3>\u539f\u59cb\u5b57\u7b26\u4e32<\/h3>\n<p>\u5728\u5f15\u53f7\u4e4b\u524d\u52a0\u4e0ar\uff0c\u4f7f\u4ed6\u6210\u4e3a\u539f\u59cb\u5b57<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print(r&#039;That is Carol\\&#039; cat&#039;)\nThat is Carol\\&#039; cat<\/code><\/pre>\n<h3>\u7528\u4e09\u91cd\u5f15\u53f7\u7684\u591a\u884c\u5b57\u7b26\u4e32<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; print(&#039;&#039;&#039;Dear Tom:\nThank for your help\nTjanks;\nLili&#039;&#039;&#039;)\nDear Tom:\nThank for your help\nTjanks;\nLili<\/code><\/pre>\n<h3>\u5b57\u7b26\u4e32\u4e0b\u6807\u548c\u5207\u7247<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam = &#039;hello world!&#039;\n>&gt;&gt; spam[0]\n&#039;h&#039;\n>&gt;&gt; spam[4]\n&#039;o&#039;\n>&gt;&gt; spam[-1]\n&#039;!&#039;\n>&gt;&gt; spam[0:5]\n&#039;hello&#039;\n>&gt;&gt; spam[:5]\n&#039;hello&#039;\n>&gt;&gt; spam[6:]\n&#039;world!&#039;<\/code><\/pre>\n<h3>\u5b57\u7b26\u4e32\u4e2d\u7684in \u548cnot in \u64cd\u4f5c\u7b26<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;hello&#039; in &#039;hello word!&#039;\nTrue<\/code><\/pre>\n<h2>\u6709\u7528\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h2>\n<h3>\u5b57\u7b26\u4e32\u65b9\u6cd5 upper(), lower(), isupper(), islower()<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; spam = &#039;Hello word!&#039;\n>&gt;&gt; spam.upper()  #\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\n&#039;HELLO WORD!&#039;\n>&gt;&gt; spam = spam.lower()  #\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\n>&gt;&gt; spam\n&#039;hello word!&#039;\n>&gt;&gt; &#039;Hello&#039;.islower()  #\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u662f\u5426\u90fd\u4e3a\u5c0f\u5199\uff0c\u81ea\u52a8\u5ffd\u7565\u975e\u5b57\u6bcd\u7684\u90e8\u5206\nFalse\n>&gt;&gt; &#039;Hello&#039;.isupper()  #\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u662f\u5426\u5168\u4e3a\u5927\u5199\uff0c\u81ea\u52a8\u5ffd\u7565\u975e\u5b57\u6bcd\u7684\u90e8\u5206\nFalse<\/code><\/pre>\n<h3>isX\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<ul>\n<li>isalpha() \u8fd4\u56deTure\uff0c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5b57\u6bcd\uff0c\u5e76\u4e14\u975e\u7a7a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;hello&#039;.isalpha()\nTrue\n>&gt;&gt; &#039;hello &#039;.isalpha()\nFalse\n>&gt;&gt; &#039;hello1223&#039;.isalpha()\nFalse<\/code><\/pre>\n<ul>\n<li>isalnum() \u8fd4\u56deTure\uff0c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u548c\u5b57\u6bcd\uff0c\u5e76\u4e14\u975e\u7a7a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;hello&#039;.isalnum()\nTrue\n>&gt;&gt; &#039;hello &#039;.isalnum()\nFalse\n>&gt;&gt; &#039;h545&#039;.isalnum()\nTrue<\/code><\/pre>\n<ul>\n<li>isdecimal() \u8fd4\u56deTrue\uff0c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\uff0c\u5e76\u4e14\u975e\u7a7a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;123&#039;.isdecimal()\nTrue\n>&gt;&gt; &#039;hello123&#039;.isdecimal()\nFalse<\/code><\/pre>\n<ul>\n<li>isspace \u8fd4\u56deTrue\uff0c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u5e76\u4e14\u975e\u7a7a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">>&gt;&gt; &#039; &#039;.isspace()\nTrue<\/code><\/pre>\n<ul>\n<li>istitle() \u8fd4\u56deTrue\uff0c\u5b57\u7b26\u4e32\u4ec5\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934<\/li>\n<\/ul>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;This Is Title Case&#039;.istitle()\nTrue\n>&gt;&gt; &#039;This Is Title Case 123&#039;.istitle()\nTrue\n>&gt;&gt; &#039;This Is not Title Case&#039;.istitle()\nFalse\n>&gt;&gt; &#039;This Is NOT Title Case&#039;.istitle()\nFalse<\/code><\/pre>\n<h3>\u5b57\u7b26\u4e32\u65b9\u6cd5 startswith() \u548c endswith()<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;hello word!&#039;.startswith(&#039;Hello&#039;)\nFalse\n>&gt;&gt; &#039;hello word!&#039;.startswith(&#039;hello&#039;)\nTrue\n>&gt;&gt; &#039;hello word!&#039;.startswith(&#039;he&#039;)\nTrue\n>&gt;&gt; &#039;hello word!&#039;.startswith(&#039;h&#039;)\nTrue\n>&gt;&gt; &#039;hello word!&#039;.endswith(&#039;!&#039;)\nTrue\n>&gt;&gt; &#039;hello word!&#039;.endswith(&#039;word!&#039;)\nTrue\n>&gt;&gt; &#039;hello word!&#039;.endswith(&#039;d&#039;)\nFalse<\/code><\/pre>\n<h3>\u5b57\u7b26\u4e32\u65b9\u6cd5join() \u548csplit()<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;--&#039;.join([&#039;cats&#039;,&#039;rats&#039;,&#039;bats&#039;])  #\u7ec4\u5408\u5b57\u7b26\u4e32\n&#039;cats--rats--bats&#039;\n>&gt;&gt; &#039;My nmae is Simon&#039;.split()  #\u62c6\u5206\u5b57\u7b26\u4e32\n[&#039;My&#039;, &#039;nmae&#039;, &#039;is&#039;, &#039;Simon&#039;]<\/code><\/pre>\n<h3>\u7528rjust()\u3001ljust()\u3001center() \u65b9\u6cd5\u5bf9\u9f50<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;Hello&#039;.rjust(10)\n&#039;     Hello&#039;\n>&gt;&gt; &#039;Hello&#039;.rjust(20)\n&#039;               Hello&#039;\n>&gt;&gt; &#039;Hello&#039;.ljust(10)\n&#039;Hello     &#039;\n>&gt;&gt; &#039;Hello &#039;.center(10)\n&#039;  Hello   &#039;\n>&gt;&gt; &#039;Hello&#039;.center(5)\n&#039;Hello&#039;\n#\u5f53\u5b57\u7b26\u4e0d\u591f\u65f6\u624d\u4e0d\u586b\u5145\n>&gt;&gt; &#039;Hello&#039;.center(4)\n&#039;Hello&#039;<\/code><\/pre>\n<p>\u9ed8\u8ba4\u586b\u5145\u4e3a\u7a7a\u683c<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; &#039;Hello&#039;.rjust(20, &#039;*&#039;)\n&#039;***************Hello&#039;\n>&gt;&gt; &#039;Hello&#039;.ljust(10, &#039;-&#039;)\n&#039;Hello-----&#039;\n>&gt;&gt; &#039;Hello&#039;.center(10, &#039;+&#039;)\n&#039;++Hello+++&#039;<\/code><\/pre>\n<h3>\u7528strip()\u3001rstrip()\u3001lstrip() \u5220\u9664\u7a7a\u767d\u5b57\u7b26<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; &#039; hello word &#039;.strip()\n&#039;hello word&#039;\n>&gt;&gt; &#039; hello word&#039;.lstrip()\n&#039;hello word&#039;\n>&gt;&gt; &#039;hello word &#039;.rstrip()\n&#039;hello word&#039;<\/code><\/pre>\n<p>strip() \u62ec\u53f7\u4e2d\u7684\u5b57\u6bcd\u987a\u5e8f\u4e0d\u5f71\u54cd\u7ed3\u679c<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; spam = &#039;SpamSpamBaconSpamEggsSpamSpam&#039;\n>&gt;&gt; spam.rsplit()\n[&#039;SpamSpamBaconSpamEggsSpamSpam&#039;]\n>&gt;&gt; spam.rstrip(&#039;Spam&#039;)\n&#039;SpamSpamBaconSpamEggs&#039;\n>&gt;&gt; spam = &#039;SpamSpamBaconSpamEggsSpamSpam&#039;\n>&gt;&gt; spam.rstrip(&#039;mapS&#039;)\n&#039;SpamSpamBaconSpamEggs&#039;\n>&gt;&gt; spam = &#039;SpamSpamBaconSpamEggsSpamSpam&#039;\n>&gt;&gt; spam.rstrip(&#039;mpaS&#039;)\n&#039;SpamSpamBaconSpamEggs&#039;\n>&gt;&gt; spam = &#039;SpamSpamBaconSpamEggsSpamSpam&#039;\n>&gt;&gt; spam.strip(&#039;Spam&#039;)\n&#039;BaconSpamEggs&#039;<\/code><\/pre>\n<h3>\u7528pyperclip\u6a21\u5757\u62f7\u8d1d\u7c98\u8d34\u5b57\u7b26\u4e32<\/h3>\n<p>pyperclip()\u6a21\u5757\u6709copy()\u3001paste() \u4e24\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5411\u8ba1\u7b97\u673a\u7684\u526a\u8d34\u677f\u53d1\u9001\u6587\u672c\u3001\u6216\u8005\u4ece\u5b83\u63a5\u6536\u6587\u672c<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import pyperclip\n>&gt;&gt; pyperclip.copy(&#039;Hello&#039;)\n>&gt;&gt; pyperclip.paste()\n&#039;Hello&#039;<\/code><\/pre>\n<h4>\u5c0f\u9879\u76ee\uff1a\u53e3\u4ee4\u4fdd\u9669\u7bb1<\/h4>\n<pre><code class=\"language-python\">#! python3\n# pw.py - An insecure password locker program.\n\nPASSWORDS = {&#039;email&#039;:&#039;test@test.com&#039;,\n             &#039;blog&#039;:&#039;www.appblog.cn&#039;,\n             &#039;luggage&#039;:&#039;123456&#039; }\nimport sys,pyperclip\nif len(sys.argv)&lt;2:\n    print(&#039;Usage:python pw.py [account] - copy account password&#039;)\n    sys.exit()\n\naccount = sys.argv[1]  #\u8fd9\u91cc\u5e94\u8be5\u662faccount name\n\nif account in PASSWORDS:\n    pyperclip.copy(PASSWORDS[account])\n    print(&#039;Password for &#039; + account + &#039; is copied to clipboard.&#039;)\nelse:\n    print(&#039;There is no account named &#039;+ account)<\/code><\/pre>\n<h4>\u5c0f\u9879\u76ee\uff1a\u5728Wiki\u6807\u8bb0\u4e2d\u6dfb\u52a0\u65e0\u5e8f\u5217\u8868<\/h4>\n<pre><code class=\"language-python\">#! python3\n# bulletPointAdder.py\n\nimport pyperclip\ntext = pyperclip.paste()\n\ntemp = text.split(&#039;\\n&#039;)\nfor i in range(len(temp)):\n    temp[i] = &#039;* &#039; + temp[i]\n\ntext = &#039;\\n&#039;.join(temp)\npyperclip.copy(text)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf \u53cc\u5f15\u53f7 >&gt;&gt; spam = &quot;This is Tom&#039; cat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}