python 基礎 之 字串,python基礎字串

來源:互聯網
上載者:User

python 基礎 之 字串,python基礎字串

Str 字串

python 字串str是在Python編寫程式過程中,最常見的一種基礎資料型別 (Elementary Data Type)。

A、建立字串

a=str('字串')

a='這樣也是字串'

astr='我是字串'  #使用單引號或是雙引號來建立bint=123   #這是一個int類型bstr=str(bint)  #str()可以用來把List,dict,int,float這些類轉為字串#輸入出結果及類型print(astr,type(astr))print(bint,type(bint))print(bstr,type(bstr))

#輸出

 我是字串 <class 'str'>
 123 <class 'int'>
 123 <class 'str'>

 

 

B、三引號"""  """

1、定義字串

2、解釋說明

3、文檔字串,主要用在類(calss)裡面,使用 __doc__

atstr="""        我這裡可以這樣寫        我的排列,輸出來就這樣 我怎麼排,輸出就怎麼樣        """

輸出結果:

        我這裡可以這樣寫        我的排列,輸出來就這樣 我怎麼排,輸出就怎麼樣

 

C、python用反斜線(\)逸出字元

逸出字元    描述\(在行尾時)  續行符\\   反斜線符號\'   單引號\"   雙引號\a   響鈴\b   退格(Backspace)\e   轉義\000   空\n   換行\v   縱向定位字元\t   橫向定位字元\r   斷行符號\f   換頁\oyy   八位元,yy代表的字元,例如:\o12代表換行\xyy   十六進位數,yy代表的字元,例如:\x0a代表換行\other   其它的字元以普通格式輸出

 

1、原始字串  

使用r 表示原始字串 如:(r'C:\Python\Python36\Scripts') 

 

D、字串格式化。

1、 % 格式化符

print('我叫 %s 今年 %d 歲!' % ('小明', 10))#輸出結果:我叫 小明 今年 10 歲!

 

符   號描述%c 格式化字元及其ASCII碼%s 格式化字串%d 格式化整數%u 格式化無符號整型%o 格式化無符號八位元%x 格式化無符號十六進位數%X 格式化無符號十六進位數(大寫)%f 格式化浮點數字,可指定小數點後的精度%e 用科學計數法格式化浮點數%E 作用同%e,用科學計數法格式化浮點數%g %f和%e的簡寫%G %f 和 %E 的簡寫%p 用十六進位數格式化變數的地址

  

 2、.format() 格式化 使用到 {}(大括弧)

print('我叫 {} 今年 {} 歲!'.format('小明',10))#輸出結果:我叫 小明 今年 10 歲!

 

E、字串運算子 astr='Holle'  、bstr='Python'

1、+ 字串串連 astr+bstr

cstr=astr+bstrprint(cstr)#輸出結果:HollePython

2、* 重複輸出字串 astr*3

print(astr*3)#輸出結果:HolleHolleHolle

3、[] 通過索引擷取字串中字元 astr[0]

print(astr[0])#輸出結果:H

4、[ : ] 截取字串中的一部分 astr[1:4],從索收 1到4,不包含4

print(astr[1:4])#輸出結果:oll

正向

 

反向

 

5、[::-1]反轉字串 (-1反向)

print(astr[::-1])#輸出結果:elloH

 

6、判斷

6.1、in 成員運算子 - 如果字串中包含給定的字元返回 True

print('H'in astr)#輸出結果:True

6.2、not in 成員運算子 - 如果字串中不包含給定的字元返回 True

print('H'not in astr)#輸出結果:False#--------------------------------print('A'not in astr)#輸出結果:True

 

F、字串內建函數

1、strip()用於移除字串頭尾指定的字元(預設為空白格)另外還有 strip('需要移除的符號')

a='   $#$#holle python     'c=a.strip()d=c.strip('$#')print(c)print(d)#輸出結果:$#$#holle pythonholle python

1.1 .lstrip() 移除左邊空格

a='   lstrip   'print(a.lstrip())#輸出結果:lstrip   

1.2.rstrip()移除右邊空格

a='   lstrip   'print(a.rstrip())#輸出結果:   lstrip

2、replace(x,y,z)替換字元,傳入參數3個,前兩個是必傳,

第一個是要替換的原有子字串,第二個是需要替換的新子字串,第三個是最大的替換次數,不填寫預設全部替換

dstr='我是小明,小明的小明'print(dstr.replace('小明','小白'))print(dstr.replace('小明','小白',2))#傳入第三個參數,最大替換2次#輸出結果:我是小白,小白的小白我是小白,小白的小明

3、index()、find() 尋找字串中是否包含子字串並返回字串索引位置

3.1、index()尋找,找到則顯示字串所在的索引位置

aindex='我的名字叫小明'print(aindex.index('名'))#輸出結果:2

3.2、index()如尋找的不存在則報錯

aindex='我的名字叫小明'print(aindex.index('他'))#輸出結果:ValueError: substring not found

3.3、find()尋找,找到則顯示字串所在的索引位置(預設從左邊開始尋找)

aindex='我的名字叫小x'print(aindex.find('x'))#輸出結果:6

3.4、rfind()從右邊開始尋找,找到則顯示字串所在的索引位置

aindex='我的名字叫小x'print(aindex.rfind('x'))#輸出結果:6

3.4、 find()與rfind()如尋找不到則返回-1

aindex='我的名字叫小x'print(aindex.find('s'))#輸出結果:-1

 

E、字串比較

使用到operator需要匯入 :from operator import lt ,le,gt,ge,eq

lt 相當於: < 、le 相當於:<= 、gt 相當於:> 、ge 相當於:>= 、eq 相當於:==

*這裡例子用到字母比較,從26個字母a開始,最開始的a 是最小的,越往後越大。

ator='abc'btor='hij'print(lt(ator,btor))#輸出結果:True
print(le(ator,btor))#輸出結果:True
print(gt(ator,btor))#輸出結果:False
print(ge(ator,btor))#輸出結果:False
print(eq(ator,btor))#輸出結果:False

 

F、字串字母轉換

1、字母小寫轉大寫 upper()

abi='abcde'print(abi.upper())#字母小寫轉大寫#輸出結果:ABCDE

2、字母大寫轉小寫 lower()

 

axi='ABCDE'print(abi.lower())#字母大寫變小寫#輸出結果:abcde

3、標題化--首字母大寫 title()

axi='ABCDE'print(axi.title())#標題化,首字母大寫#輸出結果:Abcde

4、大小寫反轉 --- 小寫轉大寫,大寫轉小寫 swapcase()

abx='aBcdEfGHijKL'print(abx.swapcase())#輸出結果:AbCDeFghIJkl

5、分割字串並放入一個 list()內   ----- .split()

asp='a b c d e'print(asp.split())#輸出結果:['a', 'b', 'c', 'd', 'e']

5.1、.split(',') 分割,字串並放入一個 list()內

aspb='a,b,c,d,e'print(aspb.split())#輸出結果:['a,b,c,d,e']

 6、join()拼接字串

ajo='abcd'bjo='$'.join(ajo)#使用$連接字串print(bjo)#輸出結果:a$b$c$d

7、字串對齊 ljust() 、rjust() 、center()

ajust='python'print(ajust.ljust(50,'-'))#向右以-補對齊50#輸出結果:python--------------------------------------------
print(ajust.rjust(50,'-'))#向左以-對齊50#輸出結果:--------------------------------------------python
print(ajust.center(50,'-'))#以- 50兩向中間對齊#輸出結果:----------------------python----------------------

8、判斷字元開頭與結尾字元 startswith()、endtswith()

asta='holle python'print(asta.startswith('ho'))  #判斷以ho開頭#輸出結果:True
asta='holle python'print(asta.endswith('on'))  #判斷以on結尾#輸出結果:True

9、計算字串中的子字串出現幾次 count()

acou='xi in xi cc bbi dd'print(acou.count('xi'))#計算 xi 在acou中出現幾次#輸出結果:2

10、zfill() 返回指定長度的字串,原字串向靠右對齊,前面以0充。

ajust='python'print(ajust.zfill(10)) #返回長度為10的字串,向靠右對齊以0填充#輸出結果:000000000python

 

 

#其它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.