<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [toc] ## **字符串基礎知識** 在Python中,加了引號的字符都被認為是字符串,字符串也可以像list列表一樣通過下標引用 ```python >>> name = "Alex Li" #雙引號 >>> age = "22" #只要加引號就是字符串 >>> age2 = 22 #int >>> msg = '''My name is Alex, I am 22 years old!''' #3個引號也可以 >>> hometown = 'ShanDong' #單引號也可以 ``` ### 單雙多引號的區別 * 單雙引號的區別 木有任何區別,只有下面這種情況,需要考慮單雙的配合 `msg = "My name is Alex , I'm 22 years old!" ` * 多引號的作用呢 作用就是多行字符串必須用多引號 ``` msg = ''' 今天我想寫首小詩, 歌頌我的同桌. ''' print(msg) ``` ### 字符串拼接 字符串呢只能進行"相加"和"相乘"運算。 ```python >>> name 'Alex Li' >>> age '22' >>> >>> name + age #相加其實就是簡單拼接 'Alex Li22' >>> >>> name * 10 #相乘其實就是復制自己多少次,再拼接在一起 'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li' ``` > 注意: > 字符串的拼接只能是雙方都是字符串,不能跟數字或其它類型拼接 > 字符串的單引號和雙引號都無法取消特殊字符的含義,如果想讓引號內所有字符均取消特殊意義,在引號前面加r,如`name=r'l\thf'` ## **字符串常用方法** ### 字符串索引 ``` >>> name='hello word' >>> name[2] 'l' >>> name[1] 'e' >>> name.index('o') 4 ``` ### 字符串切片 ``` >>> name='hello word' >>> name[1:4] 'ell' >>> name[:4] 'hell' ``` ### 判斷是否純數字`isdigit` ```python >>> name1='222';name2='aaa';name3='22aa' >>> name1.isdigit(),name2.isdigit(),name3.isdigit() (True, False, False) ``` ### 字符串替換`replace` ``` >>> name='hello word omg' >>> name.replace('o','X') 'hellX wXrd Xmg' >>> name.replace('o','X',1) #只替換一個 'hellX word omg' ``` ### 字符串查找`find` ``` >>> name='hello word omg' >>> name.find('o') 4 >>> name.find('o',5) #從第5個開始查找 7 >>> name.rfind('o') #從右邊開始查找 11 ``` ### 字符串統計`count` ``` >>> name='hello word omg' >>> name.count('o') 3 >>> name.count('l') 2 ``` ### 取消前后空格`strip` ``` >>> name=' hello word omg ' >>> name.strip() 'hello word omg' ``` ### 字符串居中填補`center` ``` >>> name='hello word omg' >>> name.center(30,'-') #30個字符長度,不夠就用指定字符代替(默認空格) '--------hello word omg--------' >>> name.center(30) ' hello word omg ' ``` ### 字符串**格式化**`format` * 用`%s`格式化 ``` >>> print('my %s is %s'%('name','noah')) my name is noah >>> name='my %s is %s'%('name','noah') >>> name 'my name is noah' ``` * 用`format`格式化 ``` #1.不指定序號 >>> name="my name is {},my age is {}" >>> name.format('noah',22) 'my name is noah,my age is 22' #2.指定序號 >>> name="my name is {0},my age is {1},this is {1}" >>> name.format('noah',22,'haha') 'my name is noah,my age is 22,this is 22' #3.用變量名代替序號 >>> name="my name is {name},my age is {age}" >>> name.format(name='noah',age=22) 'my name is noah,my age is 22' ``` ### 字符串連接`join` ``` >>> name=['hello','word','ok'] >>> '+'.join(name) 'hello+word+ok' >>> '-'.join(name) 'hello-word-ok' ``` ### 字符串切割`split` ``` >>> name='hello word omg' >>> name.split() #默認用空格進行切割 ['hello', 'word', 'omg'] >>> name.split('o') #指定過切割字符 ['hell', ' w', 'rd ', 'mg'] >>> name.split('o',1) #指定切割個數 ['hell', ' word omg'] ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看