Python Slice: Start, Stop and Step

Python Zac 2 years ago (2018-02-18) 1306 views 0 comments

Description:

  • [:]    Extract all elements from List or String

 

  • [start:]    Extract elements from start to the end (“start” can change)

 

  • [:end]    Extract elements from beginning to end-1 (“end” can change)

 

  • [start:end]    Extract elements from start to end-1 (both of “start” and “end” can change )

 

  • [start:end:step]    Extract one element from start to end every other step  (start, end, step can change)

 

 

Before start:

You should know the index of Alphabet.

In our daily life, we count the numbers always start from 1 to X.

In coding, we count the numbers start from 0 to X.

So the index value of Alphabet is:

Python index code

 

 

 

 

 

 

 

 

Examples:

1. If you just use “[:]”, which means extract whole string or list.

Example 1:
 

2. Extract the twentieth index character to the last index character.

 

3. Extract the twelfth index character to fourteenth index character.

 

4. Extract the last five index characters

 

5. Extract “the twentieth index character” to “the fourth index character from the bottom” characters.

 

6. Extract “the No.6 index character from the bottom” to “the No.3 index character from the bottom”.

 

7. Extract the fourth index character to the twentieth index character. (Extract one character every 3 step.)

 

8. Extract the eighteen index character to the last character. (Extract one character every 4 step.)

 

9. Extract index character from beginning to the twenty-first index character. (Extract one index character every 5 step)

 

10. Inverted extracting

      or

 

Like (5)
Comment
Cancel comment

emoji Image Bold Strilethrough Center Italic

Your email and address

  • Name (Required)
  • Email (Required)
  • Website(Optional)