Python Forum
string slicing default value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string slicing default value
Simple answer about difference: 0 and -0 are equal. So counting from end can’t start with -0 and starts with -1. This makes all subsequent indexes different from zero-based indexing used in forward direction.

EDIT: there is subtle distinction between 0 and None:

>>> s = 'python'
>>> s[0:0]
>>> s[None:None]
>>> s[:]           # same as [None:None]
You can omit start and stop indices, Python interprets these omissions as None (or in spoken language: 'from start', 'till end').

Some rules of thumb:


- sequence is object to be sliced
- start, stop, step are indices
- start index is included in slice, end index is first not included
- start, stop and step are separated by :
- to define stop and/or step previous indices must be defined
- if value omitted Python interprets it as None ([2:] to end; [:2] from start) which is different from 0 (specific index)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.

Messages In This Thread
string slicing default value - by Uchikago - Jul-01-2019, 09:30 AM
RE: string slicing default value - by perfringo - Jul-01-2019, 11:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String slicing and loop iteration divyansh 9 2,015 Jun-07-2020, 10:29 PM
Last Post: divyansh
  String slicing divyansh 6 1,204 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty
  string slicing help oli_action 2 823 Mar-22-2020, 09:57 AM
Last Post: oli_action
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 1,278 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing Uchikago 2 1,098 Jun-28-2019, 06:35 AM
Last Post: perfringo
  String slicing in python from reverse ift38375 1 1,096 Apr-29-2019, 06:58 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 1,108 Mar-21-2019, 09:33 PM
Last Post: nilamo
  String slicing problem Ollew 4 1,451 Sep-08-2018, 08:07 PM
Last Post: Ollew
  String Slicing jack 9 5,480 May-04-2017, 11:36 AM
Last Post: buran
  Problem with slicing string YatishK 2 2,127 Apr-24-2017, 09:46 AM
Last Post: YatishK

Forum Jump:

User Panel Messages

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