Original Blog Address
1. Code
1 #strings in Pythonz can be marked with single quotes "and double quotes"2Stra ='The is string A'3StrB ="This is string B"4 Print('stra ='+stra)5 Print('StrB ='+StrB)6 7 Print('#'*50)8 #escape characters in a string9STRC ='I don\ ' t know anything'TenStrD ='\ ' yes\ ', I know.' One Print("escape characters in the string:") A Print('STRC ='+strc) - Print('StrD ='+StrD) - the Print('#'*50) - #line breaks in a string -Stre ='record my Python learning process \njava Java' - Print('Stre'+stre) + - Print('#'*50) + #String Connection ASTR1 ="A can of hot tea ' is a" atSTR2 ='A can of hot tea"'Is a' - #str2 Two pairs of ' ' recognized as two strings, merged? -STR3 ='Student' -STR4 = STR3 * 3 - Print('the string can be concatenated with \ ' +\ ' (or bonded), or it can be looped using the * number.') - Print(STR1 +STR3) in Print(STR2 +STR3) - Print(STR4) to + Print('#'*50) - #string can use Subscript (index) query theSTR5 ='http://www.cnblogs.com/yml6/' * Print(STR5) $ Print('the length of the string str5, Len (STR5) =')Panax Notoginseng Print(Len (STR5)) - Print('Str5[0] ='+str5[0]) the Print('str5[10] ='+ str5[10]) + Print('Str5[-1] = Str5[len (STR5)-1]') A Print('Str5[-1] ='+ str5[-1]) the Print('Str5[len (Stri)-1] ='+ Str5[len (STR5)-1]) + - Print('#'*50) $ #python string cannot be overwritten $ Print(str5[0]) - Print('if you make stri[0] = \ ' x\ ', an error occurs') - the Print('#'*50) - #The index can be negative, the count starts from the right, 1 is the penultimate, 2 is the second, and 0 is the first one on the left.Wuyi Print('The index can be negative, counting starts from the right') the Print('Str5[-2] ='+ str5[-2]) - Print('str5[-19:] ='+ str5[-19:]) Wu Print('Str5[0] ='+ str5[0])
View Code
2. Operation result
1>>> ================================ RESTART ================================2>>>3Stra = This isstring A4StrB = This isstring B5 ##################################################6 escape characters in the string:7STRC = I Don't know anything8StrD ='Yes', I know.9 ##################################################Ten stre record My python learning process One Java Java A ################################################## -String can be used'+'(or glue), or you can use *number Loop -A can of hot tea"' isa student theA can of hot tea isa student - studentstudentstudent - ################################################## -http://www.cnblogs.com/yml6/ +The length of the string str5, Len (STR5) = -28 +Str5[0] =h ASTR5[10] = . atStr5[-1] = Str5[len (STR5)-1] -STR5[-1] =/ -Str5[len (Stri)-1] =/ - ################################################## - h -If make stri[0] ='x', resulting in an error in ################################################## - The index can be negative, counting starts from the right toStr5[-2] = 6 +STR5[-19:] = w.cnblogs.com/yml6/ -Str5[0] =h the>>>
View Code
3, positive index, negative index
1 #Negative index should start from-12 Print('#'*50)3 Print('Slice, which treats the index as a point between two characters, the left side of the first character is 0, and the right side of the nth character in the string is index n')4 Print(' +---+---+---+---+---+ ')5 Print(' | H | e | l | P | A |')6 Print(' +---+---+---+---+---+ ')7 Print('0 1 2 3 4 5')8 Print('-6-5 -4-3 -2-1')9 Print('The first line is the position of the given 0 to 5 indices in the string, and the second row is the corresponding negative index. Slices from I to j consist of the characters between these two flags')Ten Print('for nonnegative indexes, the slice length is the difference between the two indexes. For example, the length of Word[1:3] is 2')
View Code
1 # ################################################# 2 Slice, which treats the index as a point between two characters, the left side of the first character is 0, and the right side of the nth character in the string is index n 3 4 | H | e | l | P | A | 5 6 0 1 2 3 4 7 -6- 5- 4 -3 -2 -1 8 The first line is the position of the given 0 to 5 indices in the string, and the second row is the corresponding negative index. Slices from I to j consist of the characters between these two flags 9 for nonnegative indexes, the slice length is the difference between the two indexes. For example,the length of Word[1:3] is 2
View Code
Python string _str