A basic data type for beginners of Python-string slicing

Source: Internet
Author: User
Slice truncation is a common string operation in python. we will introduce it in detail in this article. the Function of slice truncation is to obtain subcharacters or substrings. In fact, what we need to do is to use an index to separate two indexes with a colon, in the form of: variable [header subscript: tail subscript], the number before the colon represents the start position, the number after the colon indicates the end position. This is a left-closed and right-open interval. that is to say, this string contains the header subscript, but does not contain the end subscript. There are two indexing methods for Python data: 0 at the leftmost and 0 at the rightmost... slice truncation is a common string operation in python. we will introduce it in detail in this article. the Function of slice truncation is to obtain subcharacters or substrings.

In fact, what we need to do is to use an index to separate two indexes with a colon, in the form of: variable [header subscript: tail subscript], the number before the colon represents the start position, the number after the colon indicates the end position. This is a left-closed and right-open interval. that is to say, this string contains the header subscript, but does not contain the end subscript.

There are two indexing methods for Python data: the leftmost index starts with 0 and Increases Sequentially; the rightmost element index is-1 and decreases sequentially from left to left.

The indexing of Python is flexible. you can select the corresponding indexing method based on the actual situation.

String index

You can use the index to obtain a character in the string. you can use the subscript [x] directly. do not forget that the index starts from 0!
For example, if a string is "Python", you can use language [1] and lanuage [-5] to obtain 'P '.

Split slice

The slice operation in Python often uses the split slice, that is, using the colon (:) in [] to split the string.

Take say_hell = 'hello' as an example:

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.