Some basic knowledge about string objects in Python, python strings

Source: Internet
Author: User

Some basic knowledge about string objects in Python, python strings

Python strings are divided into unchangeable sequence classes, meaning that the characters contained in these strings are in the order from left to right, and they cannot be modified locally.

Basic operations

Strings can be merged by using the + operator. You can use the * operator to duplicate the strings.
 

>>>len("abc")3>>>'abc'+'def''abcdef'>>>'NI!'*4'NI!NI!NI!NI!'

A backslash "\" inside a string can be used to store strings in multiple rows.
 

>>>str = "aaa\  ....bbb\  ....ccc\  ....ddd">>>straaabbbcccddd

Indexing and partitioning

In Python, the characters in a string are extracted through indexes.
Fragment X [I: J], which indicates that "the offset in X is taken from the offset to I until the content with the offset of J is not included ". The result is a new object.
In a part, the left boundary is 0 by default, and the right boundary is the length of the part sequence by default.
 

S = 'spam' >>> S [1:] 'pam '>>>> S [: 3] 'pa' >>>> S [: -1] 'pa' >>> S [:] 'spam's [:] implements a full copy of the top-level sequence object-one with the same value, but it is the object of different memory areas. X [I: J: K] indicates "indexing the elements in the x object, from offset to I until offset is J-1, indexing every K element", the third limit K, the default value is 1, indicating a step. You can also use a negative number as a step. The function of S [:-1] is to trigger the sequence. >>> S = 'hello' >>> S [:-1] 'olleh'

String Conversion Tool

The int function converts a string to a number, and the str function converts a number to a string expression. The repr function can also convert an object to its string form. Then, these returned objects will be used as the code string and can be re-created.

Immutable

A string belongs to an immutable sequence, that is, you cannot modify a string in the same place. For example, assign a value to an index. To change a string, you need to use tools such as merging and sharding to create and assign a value to a new string. If necessary, You need to assign this result to the initial variable name of the string.
 

>>> S = 'spam' >>> S [0] = "x" # The value of S cannot be modified >>>> S = S + "spam" >>> S 'spamspam' >>> S = 'splo' >>>> S = S. replace ('pl', 'plmal')> S 'splmalot'

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.