Eg1:open (' c:\temp\tex.t ', ' A + ') Error: Because no add r,\t will be treated as an escape character (tab)
Improvements are as follows:
Open (' c:\\temp\\tex.t ', ' A + ') or------two slash
Open (R ' c:\temp\tex.t ', ' A + ')------------R represents the original string, closes the escape mechanism
Basic manipulation of strings
1, + connection
2, * repeat
3, S[i] Index
4, s[i:j] Slice slice
5, for Loop traversal
###############
1, connect + object must be a string (if other types, need to convert)
EG1:
>>> s1= "Www.baidu.com"
>>> s2= "Ronbin"
>>> Print S1,s2
www.baidu.com ronbin---------comma, output in the same line, but with spaces between them
>>> S3=S1+S2
>>> Print S3
Www.baidu.comronbin----------Full Connection
EG2:
>>> i=12
>>> s1= "Times"
>>> Print I+S1--------------types cannot be connected directly
Traceback (most recent):
File "<stdin>", line 1, in <module>
typeerror:unsupported operand type (s) for +: ' int ' and ' str '
>>> Print str (i) +s1
12times
>>> Print str (i) + ' +s1
Times
2, Repeat *
EG1:
>>> list1=[1]*5
>>> List1
[1, 1, 1, 1, 1]
>>> list2=[1, ' 3 ', 67.09]*3-------equivalent to 3 lists of connections (expands the list by 3 times times)
>>> List2
[1, ' 3 ', 67.09, 1, ' 3 ', 67.09, 1, ' 3 ', 67.09]
EG1:
>>> s= ' a '
>>> S1=s+s+s+s
>>> S1
' AAAA '
>>> s1=s*4
>>> S1
' AAAA '
>>> s1= ' a '
>>> S1
' AAAA '
When you need to connect the same string, * <======> n +
3, Index
>>> s2= ' ABCDEFD '
>>> ch=s2[3]----------[3] is called indexed index
>>> CH
' d '
Method: String_name[index]
4, sliced
Method: String_name[start_index:stop_index]
Start_index: The index of the first letter of the string to be taken
Stop_index: Index+1 to take the trailing letter of the string
EG1:
>>> s2= ' ABCDEFD '
>>> Sub=s2[1:5]
>>> Sub
' BCDE '
Start_index is omitted, it is cut from the beginning
>>> Sub=s2[:5]
>>> Sub
' ABCDE '
Stop_index omitted, then cut to the last
>>> sub=s2[1:]
>>> Sub
' BCDEFD '
Start_index and Stop_index are omitted, then full cut (also equal to not cut)
>>> sub=s2[:]
>>> Sub
' ABCDEFD '
Start_index and Stop_index can be negative
-1 for the last, 2 for the penultimate, and so on
18th: String Basic operation