Python string bit Retrieval

Source: Internet
Author: User
The string intercepted by python is an ordered collection. We can use indexes to extract the desired characters. We can also use Python strings as a list of strings for better understanding.

The Python string list has two values.
1 is from left to right index default start 0, the maximum range is less than 1 String Length
S = 'ilovepython'
S [0] results in I

2 is from right to left index default-1 start, the maximum range is the start of the string
S = 'ilovepython'
The result of S [-1] is N.

The above is to get a character, if you actually want to get a broken character, you can usePython string TruncationFunction

For example
S = 'ilovepython'
S [] returns love
When a string separated by colons is used, Python returns a new object. The result contains the continuous content identified by the offset. The start of the left part contains the lower boundary, for example
The above result contains the value L of S [1], and the maximum obtained range does not include the upper boundary, that is, the value p of S [5 ].

Here are a few examples to help you better understand:
S [: 3]
The result is 'ilo', saving: If the left side is left, the index value starting from the left side is 0. In fact, this expression is the same as s [0: 3.
S [:-1]
The result is that the index value starting from the left side is 0, and the maximum value is the last character in the string, but not the last character.
S [:]
The result is: 'ilovepython 'obtains the element from the offset 0 to the end. This is a python string copy. You can better understand Python reference and copy here.

For more information about Python strings, see: Replacement of Python strings and interception of Python strings.

Author: Lao Wang @ Python tutorial
Old Wang Python provides Python tutorials and Python downloads related to pythn.

Related Article

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.