Some basic knowledge of string objects in Python

Source: Internet
Author: User
Python strings are categorized as immutable sequences, meaning that the characters contained in these strings exist in order from left to right, and they cannot be modified locally.

Basic operations

Strings can be merged with the + operator and can be repeated using the * operator.

>>>len ("abc") 3>>> ' abc ' + ' def ' abcdef ' >>> ' ni! ' ' ni! ni! ni! ni! '

A backslash "\" inside a string allows the string to be placed in more than one line.

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

Indexes and Shards

In Python, the characters in a string are extracted by an index.
Shard X[i:j], which means "remove the content from offset to I in X, but not include the offset of J". The result is a new object is returned.
In one Shard, the left boundary defaults to 0, and the right border defaults to the length of the Shard sequence.

S = ' Spam ' >>>s[1:] ' pam ' >>>s ' Spam ' >>>s[:3] ' spa ' >>>s[:-1] ' spa ' >>>s[:] ' Spam ' s[:] Implements a complete copy of the top-level sequence object-an object that has the same value but is a different memory slice. X[I:J:K] means "index x objects in the element, from offset to I until offset to J-1, every k element index once", the third limit K, the default is 1, indicating stepping. You can also use a negative number as a stepping, s[::-1] is actually going to send the sequence. >>>s = ' Hello ' >>>s[::-1] ' Olleh '

String Conversion Tool

The Int function converts a string to a number, and the STR function converts the number to a string representation. The REPR function can also convert an object to its string form, and the returned object will be used as a string of code to recreate the object.

Non-denaturing

A string is an immutable sequence, that is, you cannot modify a string in place, for example, to assign a value to an index. To change a string, you need to create and assign a new string using a tool such as merge, Shard, and, if necessary, assign the result to the original variable name of the string.

>>>s = ' spam ' >>>s[0] = "x" #不允许修改S的值 >>>s = s + "spam" >>>s ' spamspam ' >>>s = ' s Plot ' >>>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.