Python-based data types

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.