python基礎--字串

來源:互聯網
上載者:User

標籤:art   下標   index   short   family   開頭   dex   可變   分割   

字串1、形式

  單引號括起來的字串:‘hello‘

  雙引號括起來的字串:"Hello"

  三引號括起來的字串:‘‘‘hello‘‘‘(三單引號),"""hello"""(三雙引號)

  三引號括起來的字串可以換行

2、下標(索引)

  從左往右,下標從0開始;

  從右往右,下標從-1開始

1 a = ‘abc‘ 2 print(a[0])  ## 輸出結果: a3 print(a[-1]) ## 輸出結果: c

 

3、切片-- 相當於字串的截取

  文法:字串[起始索引:結束索引:步長]  --- 不包含結束索引的字元

     列表、元祖、字串都有切片操作!!

 

  格式:     info[開始索引:結束索引]
      不包括結束索引的字元

1 info = ‘life is short, you need pyhton ‘2 print(info[0:4]) 3 print(info[5:]) ## 5到結束字元4 print(info[:10]) # 開始到10 位置5 print(info[1:10:2]) ## 2表示步長6 print(info[::2]) ##隔一個取一個7 print(info[::-1]) ## 倒序!!!
 字串的常用操作:

  無論對字串進行怎樣的操作,原字串一定是不變的!!一般是產生了新的字串

  字串的不可變性 !!

   1、strip()   ----  去除空格

1 a = ‘ abc def ‘2 print(a.strip())  ## 去除全部空格3 print(a.lstrip()) ## 去除左邊的空格4 print(a.rstrip()) ## 去除右邊的空格

  2、upper、lower --- 大小寫轉換

1 b = ‘abCDE‘2 print(a.lower())  ## 輸出結果是:abcde3 print(a.upper()) ## 輸出的結果:ABCDE4 5 ##判斷是否是大寫或小寫:6 isupper()和islower()

  3、endswith()    startswith() --- 判斷以xx開頭或者以xx結尾

1 b= ‘ASD_234‘2 print(b.endswith(‘234’)) ##輸出結果是: True3 print(b.startswith(‘AS‘)) ##輸出結果是:True

  4、join()   字串的拼接

1 print(‘‘.join([‘hello‘,‘world‘])) ##輸出的結果是:helloworld2 3 ##更加常用的拼接方法:4 a  = ‘abc‘5 b  = ‘efgr‘6 print(a+b) ## 輸出的結果是:abcefgr

  5、各種常用判斷數字、字母、空格

## 判斷是否都是數字 print(‘123‘.isdigit())  print(‘123‘.isdecimal())print(‘123‘.isnumeric())## 判斷是否是空格print(‘ ‘.isapace()) ## True## 判斷是否都是字元組成print(‘abcdf‘.isalpha())## 判斷是否是有數字和字元組成print(‘fff123‘.isalnum())

總結:以上的幾個方法都可以配合for迴圈,遍曆之後來一個個判斷所有字元的情況,用於統計字元的個數啥的

  6、split() ----- 分割

  分割後的結果不包含分割符,結果返回一個列表

1 name = ‘I love you‘2 print(name.split(‘ ‘)) ## 輸出的結果是:[‘I‘, ‘love‘, ‘you‘]3 4 ## 若是分割字元不存在,就直接將字串整體給列表5 print(name.split(‘x‘)) ##輸出的結果是:[‘I love you‘]
1 小例子:假設輸入:10*20  求計算結果2 shuzi = ‘10-20‘3 if shuzi.find(‘*‘) !=-1:     ##找到了*4     l = shuzi.split(‘*‘)     ## 分割,返回的結果是:l = [‘10‘.‘20‘]5     v = int(l[0])*int(l[1])6     print(V)                 ##輸出的結果是:200

  還有一個分割:splitlines()--- 按照分行符號進行分割!!

  7、replace()  ----- 替換

    替換之後,產生了一個新的字串,原來的字串不變

1 ## 預設替換所有指定字元2 info = ‘life is shorts‘3 print(info.replace(‘is‘,was))   ##輸出:life was shorts4 5 ## 指定替換次數6 print(info.replace(‘s‘,‘S‘,1))   ##輸出:life is Shorts 

  8、find()  ----- 尋找

  如果存在,就返回第一個字元的索引;

  如果不存在,就返回-1

 1 info = ‘life is short, you need pyhton ‘ 2 print(info.find(‘is‘)) 3 print(info.find(‘iii‘)) 4 print(info.find(‘is‘,10,20)) ##指定範圍 5  6 ##index() 也是尋找,若是不存在,會報異常 7 print(info.index(‘sss‘)) 8 ##指定範圍 9 print(info.index(‘is‘,4,20))10 11 # ##擷取指定的內容出現的次數12 # ## 不存在返回0次13 ## 也可以指定範圍14 print(info.count(‘s‘))15 print(info.count(‘s‘,10,15))

  8、format() ----- 格式化字串

    在format函數中,使用{}充當格式化操作符

 

1 print(‘{},{}‘.format(‘chuhao‘,20))        ##chuhao,202 print(‘{1},{0}‘.format(‘chuhao‘,20))      ##20,chuhao3 print(‘{1},{0},{1}‘.format(‘chuhao‘,20))  ##20,chuhao,20

 

python基礎--字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.