1. 程式人生 > >python 字符串 string

python 字符串 string

否則 索引下標 join() subst swap cde find 字母 sub

字符串 string

語法:

a = ‘hello world!‘

b = "hello world!"

常用操作:

1、乘法操作是將字符串重復輸出2遍

>>> a=‘abc‘*2    
>>> a
‘abcabc‘

2、切片操作,將字符串從索引下標2開始切片取到最後。

>>> print("helloworld"[2:])  
lloworld

3、in操作,判斷字符串abc是否存在於字符串abcdefg中,存在則返回True,不存在則返回False。

>>> ‘abc‘ in ‘abcdefg‘    
True

4、%s表示格式化輸出

>>> a=‘alex‘
>>> print("my name is %s" %a)    
my name is alex

%s 代表字符串

%d 代表數字

%f 代表浮點數

5、+(拼接)運算符,可以作為拼接字符使用,將字符串a和字符串b拼接成一個新的字符串c。

>>> a=‘my name is ‘    
>>> b=‘alex‘
>>> c=a+b     
>>> print c
my name is alex

6、.join()方法是拼接方法

>>> a="12345678"
>>> b="_".join(a)    
>>> b  
‘1_2_3_4_5_6_7_8‘

>>> c="_".join([a,b])
>>> c
‘12345678_abcdefg‘

"拼接字符".join() 可以把列表拼接成一個字符串,通過自定義的拼接字符為間隔符把列表中的元素拼接成一個字符串。

常用方法:

st = ‘hello kitty‘

1、counter()方法獲取字符e在字符串中出現的次數。

>>> st.count(‘e‘)  
1

2、capitalize()方法將字符串的首字符轉換成大寫字符。

>>>st.capitalize()  

‘Hello kitty‘

3、center()方法可以將字符串居中輸出,兩邊都用50個下劃線做補充,可以用於描述字符的居中輸出。

>>> st.center(50,‘_‘)  
‘___________________hello kitty____________________‘

4、endswith()方法用於判斷是否是以某個字符進行結尾,如果是則返回True,否則返回False。

>>> st.endswith(‘y‘)  
True

5、startswitch()方法用於判斷是否是以某個字符開頭,如果是則返回True,否則返回False。

>>> st.startswith(‘e‘)  
False
>>> st.startswith(‘h‘)
True

6、expandtabs()方法是控制字符串中tab建的空格數量,默認tab建是四個空格,通過這個方法可以將tab建的空格隨意定義,這裏定義的是20。

>>> bt=‘he\tllo kitty‘

>>> bt.expandtabs(tabsize=20)    

he llo kitty

7、find()方法用於查找這個字符元素在字符串中第一次出現的位置,並將索引位置返回。只能查詢第一次出現的位置。沒查找到不會報錯。

>>> st.find("e")    
1

8、format()方法用於格式化輸出,例如,format中的參數定義name,這裏的name會將自己的值傳遞給st變量中的{name},最後的輸出結果如下所示。

>>> st=‘hello kitty {name}‘  
>>> st.format(name=‘alex‘)
‘hello kitty alex‘

>>> st=‘hello kitty {name} is {age}‘
>>> st.format(name=‘alex‘,age=‘27‘)
‘hello kitty alex is 27‘

9、formap_map()方法也用於格式化輸出,功能和formap一樣,只不過formap_map的參數是以字典的形式進行賦值。

>>> st=‘hello kitty {name} is {age}‘

>>> st.format_map({"name":‘alex‘,"age":‘27‘})

‘hello kitty alex is 27‘

10、index()方法和find方法功能類似,也是用於查找字符在字符串中的第一次出現的位置,並且返回位置下標,不過index如果沒查到,會返回錯誤。

>>> st.index(‘e‘)
1

>>> st.index(‘4‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

11、isalnume()方法用於檢測字符串是否是字母數字,如果包含特殊字符則返回False,否則返回True。

>>> st=‘abc345‘

>>> st.isalnum()
True

12、isdigit()方法用於判斷一個字符串是否是一個數字。返回值True和False。

>>> "123".isdigit()
True
>>> ‘abc‘.isdigit()
False

13、isnumeric()方法用於判斷一個字符串是否是一個數字,功能及用法同isdigit()方法。返回值True和False。

14、isidentifier()方法用於判斷一個變量命名是否非法。返回值True和False。

>>> ‘abc‘.isidentifier()

True

>>> ‘123abc’.isidentifier()

False

15、islower()方法用於判斷字符串中是否全部為小寫,如果有大寫字符則返回False。

>>> st=‘abc‘
>>> st.islower()
True
>>> st=‘Abc‘
>>> st.islower()
False

16、isupper()方法功能同islower想法,用於判斷字符串中是否全部為大寫,如果有小寫則返回False。

>>> st=‘abc‘
>>> st.isupper()
False
>>> st=‘ABC‘
>>> st.isupper()
True

17、isspace()方法用於判斷是否全部都是空格,返回值True和False。

>>> st=‘ab c‘
>>> st.isspace()
False
>>> st=" "
>>> st.isspace()
True

18、istitle()方法用於判斷標題中的所有字符串首字符是否都是大寫,如果不是則返回False。

>>> st=‘My Title‘
>>> st.istitle()
True
>>> st=‘My title‘
>>> st.istitle()
False

19、lower()方法,用於將字符串全部轉換成小寫字符。

>>> st
‘My title‘
>>> st.lower()
‘my title‘

20、upper()方法,用於將字符串全部轉成大寫字符。

>>> st
‘My title‘
>>> st.upper()
‘MY TITLE‘

21、swapcase()方法,用於將字符串所有字符進行反轉,大寫變成小寫,小寫變成大寫。

>>> st=‘MY title‘
>>> st.swapcase()
‘my TITLE‘

22、strip()方法,用於將字符串開頭和結尾的空格和換行符全部去掉。經常用於獲取屏幕輸入的時候。

>>> st
‘ my title \n‘
>>> st.strip()
‘my title‘

23、

python 字符串 string