One, the string
Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '
2.unicode string with R must precede r, e.g. Name=ur ' L\THF '
String Common operations:
1, remove the blank
Name = " michael,egon,jack" Print (Name.strip ()) #去掉行头和尾部的空格
"Michael,egon,jack" #输出结果
2. Segmentation
Name = "Michael,egon,jack" Print (Name.split (",")) #分割, dividing a string into a list with "," as a delimiter
[' Michael ', ' Egon ', ' Jack '] #输出结果
3. Length
Name = "Michael,egon,jack"
Print (len (name)) #统计字符串的长度
#输出结果
4. Index
Name = "Michael,egon,jack"
Print (Name.index ("a")) #索引
4 #输出结果, find the subscript return value for the first corresponding value
5. Slicing
Name = "Michael,egon,jack" Print (Name[0:7]) #取出第一个名字, is in [] in the ":" Split left and right to write the corresponding subscript of the element, the default is to take the left. Remember "Gu Tou the End"
Michael #输出结果
Print (Name[8:12]) #取出第二个名字
Egon #输出结果
Print (name[-4:]) Note: Remember "Gu Tou regardless of tail "
Jack #输出结果
6. Step Size
Sequence[start:end:step]pythonsequence Slice, the first isolates the : start and end indexes, the second : isolates the end index and the step
stepis positive, the slice is left-to-right, start > end or empty if
stepis negative, right-to-left slices, start < end or null if
startand end fill in the blanks, the former indicates the beginning, the latter represents the last one, and at the same time is empty, the expression takes all. As for the direction, depending onstep
Can summarize a rule, step is to represent from left to right slices, and vice versa is right to left. Then according to the index slices in turn
Name = "Michael,egon,jack" Print (Name[0::3]) #mhlg, c
Python-based data types