歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Python3 字符串與列表常用功能

Python3 字符串與列表常用功能

一、字符串常用功能

1. capitalize(),將字符串的首字母變成大寫,其余全部置為小寫;如果字符串中有多個單詞,也只是將第一個單詞的首字母置為大寫;例: 

>>> name = 'i am kevin ChOu'
>>> ret = name.capitalize()
>>> print(ret)
I am kevin chou   

2.casefold(),將字符串全部置為小寫

>>> name = 'II am kevin ChOu'
>>> ret = name.casefold()   
>>> print(ret)             
ii am kevin chou  

3.center(),內容居於字符串總長度中間,其余部分用指定內容填充,默認無;其內部方法與例子如下:


#內部方法
 def center(self, width, fillchar=None):'''  內容居中,width:總長度;fillchar:空白處填充內容,默認無'''
        return ""
#實例
>>> name = 'kevin'
>>> ret = name.center(20,'*')
>>> print(ret)
*******kevin********
>>>

4.count(),統計子字符在指定范圍內出現的次數,默認為整個字符串,也可以指定起始的索引范圍;例:


>>> name = 'basketball'           
>>> ret = name.count('a')
>>> print(ret)         
2
>>> print(name.count('s'))
1
>>> print(name.count('a',0,5))
1


5.endswith(),是不是已'xx'字符結束;startswith(),是不是以'xx'字符開始;兩者都可以指定起始的索引范圍。例: 

>>> name = 'basketball' 
>>> print(name.endswith('l'))
True
>>> print(name.endswith('al'))
False
>>> print(name.endswith('e',0,4))
False
>>> print(name.endswith('e',0,5))
True
>>> print(name.startswith('b'))
True
>>> print(name.startswith('a',1,))
True
>>> print(name.startswith('a',2,6))
False
>>> print(name.startswith('as',0,6))
False
>>> print(name.startswith('as',1,6))
True
示例

6.expandtabs(),將tab轉換成空格,默認一個tab轉換成8個空格;

>>> name = 'ke\tvin'
>>> ret = name.expandtabs()
>>> print(ret)
ke      vin  

7.find() 返回子串在指定范圍內首次出現的位置,未查到返回-1。例如:

>>> name = 'basketball'
>>> print(name.find('a'))
1
>>> print(name.find('x'))
-1
>>> print(name.find('b',1,))
6

8.index()返回子串在指定范圍內首次出現的位置,未查到拋出異常。例如:

>>> name = 'basketball'
>>> print(name.index('a'))
1
>>> print(name.index('x'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> print(name.index('b',1,))
6

9.isalnum()判斷字符串是否全是字母和數字(要麼全是字母,要麼全是數字,要麼全是數字和字母)例如:

>>> str1 = 'x5y'
>>> str2 = 'ab4$'
>>> print(str1.isalnum())
True
>>> print(str2.isalnum())
False

10.isalpha()方法判斷字符串內容全是字母。例如:

>>> str1 = 'Myname'
>>> str2 = 'myageis25'
>>> str3 = 'my name'
>>> print(str1.isalpha())
True
>>> print(str2.isalpha())
False
>>> print(str3.isalpha())
False

11.isdecimal()和isnumeric()判斷字符串是否全是數字,該字符串必須是unicode object。例如:

>>> str1 = u'123456'
>>> str2 = u'myageis25'
>>> print(str1.isdecimal())
True
>>> print(str2.isdecimal())
False

12.isdigit()判斷字符串全部為數字。例如:

>>> str1 = '123456'
>>> str2 = 'myageis25'
>>> print(str1.isdigit())
True
>>> print(str2.isdigit())
False

13.islower()判斷字符串中所有的字母是否都是小寫。 isupper() 判斷字符串中所有的字母是否都是大寫。例如:

>>> str1 = "THIS is string example....wow!!!";
>>> print( str1.islower());
False
>>> str2 = "this is string example....wow!!!";
>>> print( str2.islower());
True

14.isspace()判斷字符串是否全是空白符,例如:

>>> str1 = "\t\n\r"
>>> print(str1.isspace())
True
>>> str2 = "this "
>>> print(str2.isspace())
False

15.istitle()判斷字符串中,每個單詞的首字母是否都是大寫。例如:

>>> name = 'My name is kevin'
>>> name1 = 'My Name Is kevin '
>>> print(name.istitle())
False
>>> print(name1.istitle())
True

16.join()通過特殊字符把字符串連接起來,例如:

>>> list1 = ['k','e','v','i','n']
>>> ret = ''.join(list1)
>>> ret1 = '*'.join(list1)
>>> print(ret)
kevin
>>> print(ret1)
k*e*v*i*n
>>> print('_'.join(('a','b','c',)))
a_b_c

17.len(str) 計算字符串的長度。

>>> str1 = 'kevin'
>>> print(len(str1))
5

18.str.lower()把所有的大寫字母轉成小寫;str.upper()把所有的小寫字母轉成大寫;swapcase() 方法是把字符串中的小寫轉成大寫,大寫轉成小寫。例如:

>>> name = 'kevin'
>>> print(name.lower())
kevin
>>> print(name.upper())
kevin
>>> print(name.swapcase())
kevin

19.lstrip()去除掉字符串左邊規定的字符,默認是空格;rstrip()去除掉字符串右邊規定的字符,默認是空格;strip()去除掉兩邊規定的字符,默認是空格。例:

>>> name = '**kevin**'
>>> print(name.rstrip('*'))
**kevin
>>> print(name.lstrip('*'))
kevin**
>>> print(name.strip('*'))
kevin

20.maketrans(),translate() 例:例子中實際上是把對應的字母替換成數字。

>>> str1 = 'my name is kevin'
>>> str_tab = str1.maketrans('mya','137')
>>> print(str1.translate(str_tab))
13 n71e is kevin

21.max()返回字符串中最大的字母。例如:

>>> str1 = "this is really a string example....wow!!!" 
>>> print(max(str1))
y

22.replace()用新字符替換舊字符,str.replace(old,new[, max]) max表示替換的個數

>>> str1 = "this is really a string example....wow!!!" 
>>> print(str1.replace('is','are'))
thare are really a string example....wow!!!
>>> print(str1.replace('is','are',1))
thare is really a string example....wow!!!  

23.rfind(),從字符串右邊開始查找,返回指定范圍內,子串最後出現的索引,找不到返回-1。例如:

>>> name = 'basketball'
>>> print(name.rfind('a'))
7
>>> print(name.rfind('a',0,5))
1

24.zfill(),用“0”從左邊進行填充。例:

>>> name = 'basketball'
>>> print(name.zfill(15))   
00000basketball

25.split()按指定的分隔符分隔字符串,最終返回一個列表。例如:

>>> name = 'My name is kevin'
>>> name1 = 'My_Name_Is_kevin'
>>> print(name.split())
['My', 'name', 'is', 'kevin']
>>> print(name1.split('_'))
['My', 'Name', 'Is', 'kevin']

26.title() 把字符串中每個單詞的首字母大寫。例如:

>>> name = 'my name is kevin'
>>> print(name.title())
My Name Is kevin

27.partition(),將字符串在指定分割符處分割成由前、中、後三個部分組成的元組

>>> name = 'basketball'
>>> print(name.partition('ke'))
('bas', 'ke', 'tball')

28.format(),格式化字符串,例:

>>> str1 = 'my {0} {1} {username}.'
>>> print(str1.format('name','is',username='kevin'))
my name is kevin.

二、list 常用功能

1.apend(),將元素添加進list中

>>> list1 = ['a','b','c','d','e']
>>> list1.append('f')
>>> print(list1)
['a', 'b', 'c', 'd', 'e', 'f']
>>> 

  2.clear(),清空列表

>>> list1 = ['a','b','c','d','e']
>>> list1.clear()
>>> print(list1)
[]

  3.count(),統計列表中某個元素出現的次數

>>> list1 = ['a','b','c','d','a']
>>> print(list1.count('a'))
2

  4.extend(),用另一個list來擴充一個列表

>>> list1 = ['a','b']
>>> list2 = ['c','d','f']
>>> list1.extend(list2)
>>> print(list1)
['a', 'b', 'c', 'd', 'f']
>>> 

  5.index(),找出指定范圍內某個元素的索引位置,未找到拋出異常

>>> list1 = ['a','b','c','d','e']
>>> print(list1.index('b'))
1
>>> print(list1.index('f'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'f' is not in list

  6.insert(),在指定的索引位置,插入元素

>>> list1 = ['a','b','c','d','e']
>>> list1.insert(1,'kevin')
>>> print(list1)
['a', 'kevin', 'b', 'c', 'd', 'e']

  7.pop(),刪除元素,並返回被刪除的值,默認刪除列表最後一個元素,也可以指定索引

>>> list1 = ['a','b','c','d','e']
>>> list1.pop()
'e'
>>> print(list1)
['a', 'b', 'c', 'd']
>>> list1.pop(0)
'a'
>>> print(list1)
['b', 'c', 'd']

  8.remove(),刪除指定元素

>>> list1 = ['a','b','c','d','e']
>>> list1.remove('a')
>>> print(list1)
['b', 'c', 'd', 'e']

  9.reverse(),反轉列表

>>> list1 = ['a','b','c','d','e']
>>> list1.reverse()
>>> print(list1)
['e', 'd', 'c', 'b', 'a']

  10.sort(),對列表進行排序,字符串跟數字不能直接進行排序

>>> list1 = ['a','d','b','c']
>>> list1.sort()
>>> print(list1)
['a', 'b', 'c', 'd']
>>>
>>> list2 = ['a',1,'2','d']
>>> list2.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

Ubuntu 14.04安裝Python 3.3.5  http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源碼安裝Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved