Python Forum
Slicing syntax question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing syntax question
#3
First, that comma has no place there.

Slicing... Let we have a string.

my_string = 'I feel good.'

So, being a string it has indices. my_string[0] gives us 'I' because in Python the indexing starts at 0 and 'I' is the first character. my_string[-1] gives us the period at the end of the string. The negative index means that we count backward. -1 is 'd', -2 'o' and -4 is 'g'.

The slicing: string[start :stop ]

Starting index is what you are thinking. The slicing starts from there. The stop index means it stops there but it is exclusive.

my_string[0:6] gives us 'I feel'. Well, if you count the characters they are six ( including space ). But we start from 0 so 0 to 5 is exactly 6 characters.
If we need to slice an iterable from the start we can miss the 0 index. Our example becomes my_string[:6] which gives us again 'I feel'. It is the same if we want to slice to the end. my_string[7:] give us 'good'. [:] is the whole string and [2:6] - 'feel'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Slicing syntax question - by Oliver - Dec-13-2017, 12:35 PM
RE: Slicing syntax question - by mpd - Dec-13-2017, 01:05 PM
RE: Slicing syntax question - by wavic - Dec-13-2017, 01:20 PM
RE: Slicing syntax question - by Oliver - Dec-13-2017, 02:03 PM
RE: Slicing syntax question - by hshivaraj - Dec-13-2017, 02:24 PM
RE: Slicing syntax question - by DeaD_EyE - Dec-13-2017, 03:58 PM
RE: Slicing syntax question - by metulburr - Dec-13-2017, 04:14 PM
RE: Slicing syntax question - by Oliver - Dec-13-2017, 08:24 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020