>Then Peter came to him and said,"Lord, how many times must I forgive my brother who sins against me? As many as seven times?" Jesus said to him,"Not seven times, I tell you, but seventy-seven times?" (MATTHEW 18:21-22)
#å顾å表åå符串
å表åå符串两ç§ç±»åçå¯¹è±¡ï¼æä¸å°ç¸ä¼¼çå°æ¹ï¼ä¹æå¾å¤§çåºå«ã
æ¬è®²å¯¹å¥¹ä»¬å个ç®è¦æ¯è¾ï¼åæ¶ä¹æ¯å¯¹å颿å
³ä¸¤è
çç¥è¯å¤ä¹ ä¸ä¸ï¼æè°â温æ
èç¥æ°âã
##ç¸åç¹
###齿¯åºå
ä¸ç®¡æ¯ç»æå表çå
ç´ ï¼è¿æ¯ç»æå符串çå符ï¼é½å¯ä»¥ä»å·¦åå³ï¼ä¾æ¬¡ç¨`0, 1, 2, ...`è¿æ ·çæ¹å¼å»ºç«ç´¢å¼ãèè¦å¾å°ä¸ä¸ªæå¤ä¸ªå
ç´ ï¼å¯ä»¥ä½¿ç¨åçã
å
³äºåºåçåºæ¬æä½ï¼å¯¹ä¸¤è
é½éç¨ã
ä¾å¦ï¼
>>> welcome_str = "Welcome you"
>>> welcome_str[0]
'W'
>>> welcome_str[1]
'e'
>>> welcome_str[len(welcome_str)-1]
'u'
>>> welcome_str[:4]
'Welc'
>>> a = "python"
>>> a * 3
'pythonpythonpython'
>>> git_list = ["qiwsir","github","io"]
>>> git_list[0]
'qiwsir'
>>> git_list[len(git_list)-1]
'io'
>>> git_list[0:2]
['qiwsir', 'github']
>>> b = ['qiwsir']
>>> b * 7
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']
å¯¹äºæ¤ç±»æ°æ®ï¼ä¸é¢ä¸äºæä½æ¯ç±»ä¼¼çï¼
>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str #ç¨+å·è¿æ¥str
'hello,world,Welcome you'
>>> welcome_str #忥çstr没æåå°å½±åï¼å³ä¸é¢ç+å·è¿æ¥åéæ°çæäºä¸ä¸ªå符串
'Welcome you'
>>> first
'hello,world'
>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list #ç¨+å·è¿æ¥listï¼å¾å°ä¸ä¸ªæ°çlist
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['python']
>>> len(welcome_str) #å¾å°å符æ°
11
>>> len(git_list) #å¾å°å
ç´ æ°
3
##åºå«
å表åå符串çæå¤§åºå«æ¯ï¼å表æ¯å¯ä»¥æ¹åçï¼å符串æ¯ä¸å¯åãè¿ä¸ªæä¹çè§£å¢ï¼
é¦å
ç对å表çè¿äºæä½ï¼å
¶æ ¹æºå¨äºå表å¯ä»¥è¿è¡ä¿®æ¹ï¼å³å表æ¯å¯åçã
>>> git_list = ['qiwsir', 'github', 'io']
>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']
>>> git_list[1]
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']
>>> git_list.insert(1, "algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']
>>> git_list.pop()
'python'
>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']
以ä¸è¿äºæä½ï¼å¦æç¨å¨å符串ä¸ï¼é½ä¼æ¥éï¼æ¯å¦ï¼
>>> welcome_str
'Welcome you'
>>> welcome_str[1]='E'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
>>> del welcome_str[1]
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object doesn't support item deletion
>>> welcome_str.append("E")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'append'
妿è¦ä¿®æ¹ä¸ä¸ªstrï¼ä¸å¾ä¸è¿æ ·ã
>>> welcome_str
'Welcome you'
>>> welcome_str[0]+"E"+welcome_str[2:] #仿°çæä¸ä¸ªstr
'WElcome you'
>>> welcome_str #å¯¹åæ¥çæ²¡æä»»ä½å½±å
'Welcome you'
å
¶å®ï¼å¨è¿ç§åæ³ä¸ï¼ç¸å½äºéæ°çæäºä¸ä¸ªstrã
##å¤ç»´list
è¿ä¸ªä¹åºè¯¥ç®æ¯ä¸¤è
çåºå«äºï¼è½ç¶æç¹çµå¼ºã
å¨å符串éé¢çæ¯ä¸ªå
ç´ åªè½æ¯å符ï¼å¨å表ä¸ï¼å
ç´ å¯ä»¥æ¯ä»»ä½ç±»åçæ°æ®ãåé¢è§ç夿¯æ°åæè
å符ï¼å
¶å®è¿å¯ä»¥è¿æ ·ï¼
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
è¿ä¸ªå表çå
ç´ ï¼æ¯å¦å¤ä¸ä¸ªå表ãè¿æ ·çå表ï¼ç§°ä¹ä¸ºå¤ç»´å表ãå¦æè¯»è
å¦ä¹ è¿è¡åå¼ï¼è¿å°±æ¯è¾å®¹æçè§£äºã
>>> matrix[0][1]
2
å½ç¶ï¼å表ä¹å¯ä»¥æ¯è¿æ ·çï¼
>>> mult = [[1,2,3],['a','b','c'],'d','e']
>>> mult
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
>>> mult[1][1]
'b'
>>> mult[2]
'd'
å¨å¤ç»´çæ
åµä¸ï¼éé¢çlistè¢«å½æä¸ä¸ªå
ç´ å¯¹å¾
ã
##å表åå符串转å
符åæäºæ¡ä»¶çæ
åµä¸ï¼å¯ä»¥å®ç°å表åå符串ä¹é´ç转åãä¼ä½¿ç¨å°`split()`å`join()`ï¼å¯¹è¿ä¸¤ä¸ªå½æ°ï¼å·²ç»ä¸éçäºï¼å¨åé¢å符串é¨åå·²ç»è§è¿ã
ä¸åçï¼äºåçï¼è¿æ¬¡åè§é¢ï¼ç¹å«æ¯å¨å·²ç»å¦ä¹ äºå表çåºç¡ä¸ï¼åºè¯¥ææ´æ·±å»ççè§£ã
###str.split()
è¿ä¸ªå
ç½®å½æ°å®ç°çæ¯å°str转å为listãå
¶ä¸str=""æ¯åé符ã
å¨çä¾åä¹åï¼è¯·çå®å¨äº¤äºæ¨¡å¼ä¸åå¦ä¸æä½ï¼
>>>help(str.split)
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string 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.
ä¸ç®¡æ¯å¦çæä¸é¢è¿æ®µè¯ï¼é½å¯ä»¥çä¾åãè¿æ¯å¸æè½å¤çè§£ä¸é¢çå
容ã
>>> line = "Hello.I am qiwsir.Welcome you."
>>> line.split(".") #ä»¥è±æçå¥ç¹ä¸ºåé符ï¼å¾å°list
['Hello', 'I am qiwsir', 'Welcome you', '']
>>> line.split(".", 1) #è¿ä¸ª1,å°±æ¯è¡¨è¾¾äºä¸æä¸çï¼If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']
>>> name = "Albert Ainstain" #乿å¯è½ç¨ç©ºæ ¼æ¥å为åé符
>>> name.split(" ")
['Albert', 'Ainstain']
ä¸é¢çä¾åï¼è®©ä½ æ´æç¹æå¥äºã
>>> s = "I am, writing\npython\tbook on line" #è¿ä¸ªåç¬¦ä¸²ä¸æç©ºæ ¼ï¼éå·ï¼æ¢è¡\nï¼tab缩è¿\t 符å·
>>> print s #è¾åºä¹åçæ ·å¼
I am, writing
python book on line
>>> s.split() #ç¨split(),使¯æ¬å·ä¸ä¸è¾å
¥ä»»ä½åæ°
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
妿split()ä¸è¾å
¥ä»»ä½åæ°ï¼æ¾ç¤ºå°±æ¯è§å°ä»»ä½åå²ç¬¦å·ï¼å°±ç¨å
¶åå²äºã
###"[sep]".join(list)
joinå¯ä»¥è¯´æ¯splitçéè¿ç®ï¼æ¿æ¥åé¢çæä½ï¼
>>> name
['Albert', 'Ainstain']
>>> "".join(name) #å°listä¸çå
ç´ è¿æ¥èµ·æ¥ï¼ä½æ¯æ²¡æè¿æ¥ç¬¦ï¼è¡¨ç¤ºä¸ä¸ªä¸ä¸ªç´§é»ç
'AlbertAinstain'
>>> ".".join(name) #ä»¥è±æçå¥ç¹åä¸ºè¿æ¥åé符
'Albert.Ainstain'
>>> " ".join(name) #ä»¥ç©ºæ ¼åä¸ºè¿æ¥çåé符
'Albert Ainstain'
åå°ä¸é¢é£ä¸ªç¥å¥çä¾åä¸ï¼å¯ä»¥è¿ä¹ä½¿ç¨join.
>>> s = "I am, writing\npython\tbook on line"
>>> print s
I am, writing
python book on line
>>> s.split()
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
>>> " ".join(s.split()) #éæ°è¿æ¥ï¼ä¸è¿æä¸ç¹éæ¾ï¼amåé¢éå·è¿æ¯æçãæä¹å»æï¼
'I am, writing python book on line'
读è
æ¯å¦æå°æ°å¥ï¼å¯¹äº`join()`彿°ï¼å
¶æ ¼å¼æ¯`"sep".join(list)`ï¼ä¸æ¯`list.join(sep)`ãå
¶å®ï¼`join()`æ¯åç¬¦ä¸²çæ¹æ³ï¼ä¸æ¯åè¡¨çæ¹æ³ã
>>> help(str.join)
Help on method_descriptor:
join(...)
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.
ä¸è¿ï¼è½ä¼ å
¥`join()`çå¯¹è±¡ï¼æè
è¯´åæ°çå¼ï¼ä¹æ¯ææ¡ä»¶çãä¸é¢çå°±ä¸è¡ã
>>> a = [1,2,3,'a','b','c']
>>> "+".join(a)
Traceback (most recent call last):
File "", line 1, in
"+".join(a)
TypeError: sequence item 0: expected str instance, int found
âå表æ¯è¦åâï¼ä½æ¯æä¸è®©å®å¹²è¿ä¹å¤ãå 为æ´å¤ç±»åçå¯¹è±¡ä¾æ¬¡ç»åºï¼å
让å®å°åå°ã
------
[æ»ç®å½](./index.md) | [ä¸èï¼å表(3)](./113.md) | [ä¸èï¼å
ç»](./115.md)
å¦æä½ è®¤ä¸ºæå¿
è¦æèµæï¼è¯·éè¿æ¯ä»å®ï¼**[email protected]**,ä¸èææ¿ã