Python内置数据结构2
2017-10-30 00:00
375 查看
摘要: 字符串及其操作
\ 转义符号
r'' 代表字符串是自然字符串
string的下标操作
join
split, partition
upper(), lower()
replace, strip
find, index, count
三个引号(单双)可以定义多行字符串
r''表示是一段自然字符串
find 查找,字串不存在时,返回 -1
index 查找,字串不存在时,返回ValueError
字符串及其操作
string 的定义(''' 三个引号)\ 转义符号
r'' 代表字符串是自然字符串
string的下标操作
join
split, partition
upper(), lower()
replace, strip
find, index, count
一、字符串定义:
In [1]: s = 'miracle young' In [2]: s = "miracle young" In [3]: s = '''miracle young'''
三个引号(单双)可以定义多行字符串
In [4]: s = ''' ...: miracle young ...: is handsome ...: ''' In [5]: s = ' File "<ipython-input-5-5a20d2d986f5>", line 1 s = ' ^ SyntaxError: EOL while scanning string literal In [6]:
二、字符串相关操作
1、转义 (\) or (r'')
In [13]: s = 'miracle young's wife very happy' File "<ipython-input-13-312ae38bd009>", line 1 s = 'miracle young's wife very happy' ^ SyntaxError: invalid syntax In [14]: s = 'miracle young\'s wife very happy' In [15]: s Out[15]: "miracle young's wife very happy" In [16]: print(s) miracle young's wife very happy In [17]:
In [7]: path = 'C:\windows\nt\system32' In [8]: print(path) C:\windows t\system32 In [9]: path = r'C:\windows\nt\system32' In [10]: print(path) C:\windows\nt\system32 In [11]:
r''表示是一段自然字符串
2、下标操作
In [11]: s = 'miracle young papapapa' In [12]: for ss in s: ...: print(ss) ...: m i r a c l e y o u n g p a p a p a p a In [13]: s[0] Out[13]: 'm' In [14]: s[2] Out[14]: 'r' In [15]: s[-1] Out[15]: 'a' In [16]: s[-100] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-16-3eaddaa60d3d> in <module>() ----> 1 s[-100] IndexError: string index out of range In [17]: s[100] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-17-2a138df92e52> in <module>() ----> 1 s[100] IndexError: string index out of range In [18]:
In [18]: c = list(s) In [19]: c Out[19]: ['m', 'i', 'r', 'a', 'c', 'l', 'e', ' ', 'y', 'o', 'u', 'n', 'g', ' ', 'p', 'a', 'p', 'a', 'p', 'a', 'p', 'a'] In [20]: len(s) Out[20]: 22 In [21]: s.count('m') Out[21]: 1 In [22]: s.index('m') Out[22]: 0 In [23]: s + ' wife' Out[23]: 'miracle young papapapa wife' In [24]:
3、join
In [27]: l = ['miracle', 'young', 'papapa'] In [28]: new_s = ' '.join(l) #返回结果,原地不做修改 In [29]: new_s Out[29]: 'miracle young papapa' In [30]: new_s = ','.join(l) In [31]: new_s Out[31]: 'miracle,young,papapa' In [32]:
4、分隔
1) s.split()
In [43]: help(s.split) Help on built-in function split: split(...) method of builtins.str instance S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. ~ (END)
In [48]: s = ' papa pa pa ' In [49]: s.split() # 返回结果, 原地不做修改 Out[49]: ['papa', 'pa', 'pa'] In [50]: s Out[50]: ' papa pa pa ' In [51]: s.split(' ', 1) Out[51]: ['', 'papa pa pa '] In [52]: s.split(' ') Out[52]: ['', 'papa pa pa', ' '] In [53]: s.split('pa') # split 的分隔符可以是任意多个字符串 Out[53]: [' ', '', ' ', ' ', ' '] In [54]:
def _split(s, sep, maxsplit): ret = [] tmp = [] i = 0 for c in s: if c!= sep: tmp.append(c) else: i += 1 ret.append(''.join(tmp)) tmp.clear() if i >= maxsplit and maxsplit > 0: return ret return ret
2) rsplit()
In [48]: s = ' papa pa pa ' In [49]: s.split() # 和s.split() 结果一样 从右开始分隔 Out[49]: ['papa', 'pa', 'pa'] In [50]: s Out[50]: ' papa pa pa ' In [51]: s.split(' ', 1) Out[51]: ['', 'papa pa pa '] In [52]: s.split(' ') Out[52]: ['', 'papa pa pa', ' '] In [53]: s.split('pa') Out[53]: [' ', '', ' ', ' ', ' '] In [54]: s.rsplit() Out[54]: ['papa', 'pa', 'pa'] In [55]: s.rsplit(' ', 1) Out[55]: [' papa pa pa ', ''] In [56]:
def _rsplit(s, sep, maxsplit): ret = [] tmp = [] i = 0 for c in reversed(s): if c!= sep: tmp.append(c) else: i += 1 ret.append(''.join(reversed(tmp))) tmp.clear() if i >= maxsplit and maxsplit > 0: return reversed(ret) return reversed(ret)
3) spitlines()
In [1]: sql = ''' ...: sfsdf ...: sdfsdfsdfsdf ...: ...: sdfsdfsdfsf ...: ...: sdfsfsdfsdfsfsfsd ...: ''' In [2]: sql.splitlines() Out[2]: ['', 'sfsdf', 'sdfsdfsdfsdf', '', 'sdfsdfsdfsf', '', 'sdfsfsdfsdfsfsfsd'] In [3]: sql = ''' ...: sfsdf ...: sdfsdfsdfsdf ...: sdfsdfsdfsf ...: sdfsfsdfsdfsfsfsd ...: ''' In [4]: sql.splitlines() Out[4]: ['', 'sfsdf', 'sdfsdfsdfsdf', 'sdfsdfsdfsf', 'sdfsfsdfsdfsfsfsd'] In [5]:
4)partition
In [6]: s = 'host = 127.0.0.1' In [7]: help(s.partition) Help on built-in function partition: partition(...) method of builtins.str instance S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings. ~ (END)
In [8]: s.split('=') Out[8]: ['host ', ' 127.0.0.1'] In [9]: s.partition('=') Out[9]: ('host ', '=', ' 127.0.0.1') In [10]: cfg = 'mysql.connect = mysql://user:password@127.0.0.1:3306/test' In [11]: cfg.partition('=') Out[11]: ('mysql.connect ', '=', ' mysql://user:password@127.0.0.1:3306/test') In [12]: cfg = 'env=PATH=/sur/bin:$PATH' In [13]: cfg.partition('=') Out[13]: ('env', '=', 'PATH=/sur/bin:$PATH') In [14]:
5)rpartition
In [14]: s = 'a=b=c' In [15]: s.partition('=') Out[15]: ('a', '=', 'b=c') In [16]: s.rpartition('=') Out[16]: ('a=b', '=', 'c') In [17]:
In [17]: help(s.rpartition) Help on built-in function rpartition: rpartition(...) method of builtins.str instance S.rpartition(sep) -> (head, sep, tail) Search for the separator sep in S, starting at the end of S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return two empty strings and S. ~ (END)
def _partion(s, sep): if s == '': return '', '', '' tmp = s.split(sep, maxsplit=1) if (len(tmp)) == 2: return tmp[0], sep, tmp[1] elif len(tmp) == 1: return tmp[0], sep, '' elif len(tmp) == 0: return '', sep, ''
5、大小写转换
In [18]: s = 'miracle' In [19]: s.upper() Out[19]: 'MIRACLE' In [20]: s.lower() Out[20]: 'miracle' In [22]: s.casefold() Out[22]: 'miracle' In [23]: s.swapcase() # 大小写互换 Out[23]: 'MIRACLE' In [24]:
6、排版
In [25]: s Out[25]: 'miracle' In [26]: s.title() Out[26]: 'Miracle' In [27]: s = 'miracle young like dota and basketball.' In [28]: s.title() Out[28]: 'Miracle Young Like Dota And Basketball.' In [29]: s.capitalize() Out[29]: 'Miracle young like dota and basketball.' In [30]: s = 'miracle young like dota and basketball and Girl.' In [31]: s.capitalize() Out[31]: 'Miracle young like dota and basketball and girl.'
In [32]: s.center(100) Out[32]: ' miracle young like dota and basketball and Girl. ' In [33]: help(s.center) Help on built-in function center: center(...) method of builtins.str instance S.center(width[, fillchar]) -> str Return S centered in a string of length width. Padding is done using the specified fill character (default is a space) ~ (END)
In [34]: s.center(100, '#') Out[34]: '##########################miracle young like dota and basketball and Girl.##########################' In [35]:
In [35]: help(s.zfill) Help on built-in function zfill: zfill(...) method of builtins.str instance S.zfill(width) -> str Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated. ~ (END)
In [36]: s Out[36]: 'miracle young like dota and basketball and Girl.' In [37]: s.zfill(100) Out[37]: '0000000000000000000000000000000000000000000000000000miracle young like dota and basketball and Girl.' In [38]: str(1234).zfill(11) Out[38]: '00000001234' In [40]: '\t'.expandtabs() Out[40]: ' ' In [41]:
7、修改
1)s.replace()
In [41]: help(s.replace) Help on built-in function replace: replace(...) method of builtins.str instance S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. ~ (END)
In [42]: s = 'abcdefgdscesabcbc' In [43]: s.replace('e', 'x') # 返回结果集,原地不做修改。 Out[43]: 'abcdxfgdscxsabcbc' In [44]: s Out[44]: 'abcdefgdscesabcbc' In [45]: s.replace('e', 'X', 1) Out[45]: 'abcdXfgdscesabcbc' In [46]: s.replace('e', 'X', 2) Out[46]: 'abcdXfgdscXsabcbc' In [47]: s.replace('abc', 'XYZ') Out[47]: 'XYZdefgdscesXYZbc' In [48]:
2) s.strip()
In [48]: help(s.strip) Help on built-in function strip: strip(...) method of builtins.str instance S.strip([chars]) -> str Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. ~ (END)
In [49]: s = ' pa pa pa ' In [50]: s.strip() Out[50]: 'pa pa pa' In [51]: s.strip(' ') Out[51]: 'pa pa pa' In [52]: s.strip(' ') Out[52]: 'pa pa pa' In [53]: s = 'ababbcased' In [54]: s.strip('abd') Out[54]: 'case'
3) s.lstrip() and s.rstrip()
In [55]: help(s.lstrip) Help on built-in function lstrip: lstrip(...) method of builtins.str instance S.lstrip([chars]) -> str Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. ~ (END)
In [59]: help(s.rstrip) Help on built-in function rstrip: rstrip(...) method of builtins.str instance S.rstrip([chars]) -> str Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. ~ (END)
In [56]: s Out[56]: 'ababbcased' In [57]: s.lstrip('ad') Out[57]: 'babbcased' In [58]: s.rstrip('ad') Out[58]: 'ababbcase' In [59]:
4) s.ljust() and s.rjust()
In [64]: help(s.ljust) Help on built-in function ljust: ljust(...) method of builtins.str instance S.ljust(width[, fillchar]) -> str Return S left-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space). ~ (END)
In [65]: help(s.rjust) Help on built-in function rjust: rjust(...) method of builtins.str instance S.rjust(width[, fillchar]) -> str Return S right-justified in a string of length width. Padding is done using the specified fill character (default is a space). ~ (END)
In [60]: s Out[60]: 'ababbcased' In [61]: s.ljust(10) Out[61]: 'ababbcased' In [62]: s.ljust(100) Out[62]: 'ababbcased ' In [63]: s.rjust(100) Out[63]: ' ababbcased' In [64]:
In [66]: s.ljust(100, '#') Out[66]: 'ababbcased##########################################################################################' In [67]: s.rjust(100, '#') Out[67]: '##########################################################################################ababbcased' In [68]: s.rjust(100, '##') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-68-b458ded36f17> in <module>() ----> 1 s.rjust(100, '##') TypeError: The fill character must be exactly one character long In [69]:
8、查找
1) s.find()
In [69]: s = 'i love miracle young love nice girl' In [70]: help(s.find) Help on built-in function find: find(...) method of builtins.str instance S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. ~ (END)
In [71]: s.find('love') # 返回第一个匹配的字符串,返回的值是字串的第一个字符索引 Out[71]: 2 In [72]: s.find('love', 3) Out[72]: 21 In [73]: s.find('love', 3, 5) # 在索引 start 3 end 5 范围内查找 'love'没有找到则返回 -1 Out[73]: -1 In [74]:
2) s.rfind()
In [74]: help(s.rfind) Help on built-in function rfind: rfind(...) method of builtins.str instance S.rfind(sub[, start[, end]]) -> int Return the highest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. ~ (END)
In [75]: s Out[75]: 'i love miracle young love nice girl' In [76]: s.rfind('love') # 从右往左查找, 返回的还是正常的整数索引 Out[76]: 21 In [77]: s.index('love') Out[77]: 2 In [78]: s.index('love', 3) Out[78]: 21 In [79]: s.index('love', 3, 5) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-79-b1b620f7f23e> in <module>() ----> 1 s.index('love', 3, 5) ValueError: substring not found In [80]: s.rindex('love') Out[80]: 21 In [81]: s.rindex('love', 3) Out[81]: 21 In [82]: s.rindex('love', 3, 5) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-82-79f500d583eb> in <module>() ----> 1 s.rindex('love', 3, 5) ValueError: substring not found In [83]:
find 查找,字串不存在时,返回 -1
index 查找,字串不存在时,返回ValueError
3) s.count()
In [84]: s Out[84]: 'i love miracle young love nice girl' In [85]: help(s.count) Help on built-in function count: count(...) method of builtins.str instance S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. ~ (END)
In [86]: s.count('very') Out[86]: 0 In [87]: s.count('a') Out[87]: 1 In [88]: s.count('o') Out[88]: 3
4) s.startswith() and s.endswith()
In [92]: help(s.startswith) Help on built-in function startswith: startswith(...) method of builtins.str instance S.startswith(prefix[, start[, end]]) -> bool Return True if S starts with the specified prefix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try. ~ (END)
In [93]: help(s.endswith) Help on built-in function endswith: endswith(...) method of builtins.str instance S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. suffix can also be a tuple of strings to try. ~ (END)
In [94]: s Out[94]: 'i love miracle young love nice girl' In [95]: s.startswith('i love') Out[95]: True In [96]: s.endswith('girl') Out[96]: True In [97]: s.endswith('girl.') Out[97]: False In [98]:
5) s.isalnum() s.istitle() s.isdentifier()
In [98]: help(s.isalnum) Help on built-in function isalnum: isalnum(...) method of builtins.str instance S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise. ~ (END)
In [99]: s.isalnum() Out[99]: False In [100]: '1234'.isalnum() Out[100]: True In [101]: 'a1234'.isalnum() Out[101]: True In [102]: 'a1a'.isalnum() Out[102]: True
In [103]: help(s.istitle) Help on built-in function istitle: istitle(...) method of builtins.str instance S.istitle() -> bool Return True if S is a titlecased string and there is at least one character in S, i.e. upper- and titlecase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise. ~ (END)
In [107]: s.istitle() Out[107]: False In [108]: s.title().istitle() Out[108]: True In [109]:
In [113]: help(s.isidentifier) Help on built-in function isidentifier: isidentifier(...) method of builtins.str instance S.isidentifier() -> bool Return True if S is a valid identifier according to the language definition. Use keyword.iskeyword() to test for reserved identifiers such as "def" and "class". ~ (END)
In [114]: s.isidentifier() Out[114]: False In [115]: 'a_b'.isidentifier() Out[115]: True In [116]:
相关文章推荐
- week02_python内置数据结构__列表
- python学习3-内置数据结构3-字符串
- 简明Python教程读书笔记-7 几种Python内置的数据结构
- week02_python内置数据结构__随机数
- python学习3-内置数据结构2-元组
- python内置的数据结构
- Python开发技术详解-笔记_第04章-内置数据结构
- python基础知识——内置数据结构(列表)
- week02_python内置数据结构__元组
- Python内置数据结构——字典dict
- python 内置数据结构的基本操作 —— dict(2)
- 004-Python内置数据结构-七种数据结构一览
- python学习二----内置数据结构
- 【Python】06、python内置数据结构之列表和元祖
- 【Python】07、python内置数据结构之字符串
- python内置数据结构之str
- python3 一些经常使用的数据结构和内置方法。
- 【Python3】04、内置数据结构
- python 内置数据结构的基本操作 —— Set(2)
- python 3 一些常用的内置数据结构介绍