Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String slicing
#5
(May-30-2020, 09:30 PM)divyansh Wrote: # my question is that why second one is showing a output ( if you say that slicing includes the start index then what happened with the var1 output it is also starting with the same index)

The second index must be "beyond" the first index to show anything. In other words, it must lie in the correct step direction from the first index..

In your first instance, you are stepping forward (step = 1), but the second index is rearward (-16 is more rearward than -1). Because of this, no portion is selected.

In the second case, you are stepping forward again, but now 16 is beyond -1 (the last index of the string is 14). So displaying index 14 (same as index -1) is allowed.

These are all equivalent
# First index is in range, second index is in step direction.
>>> str1[14:100]
'i'
>>> str1[-1:100]
'i'
And these are equivalent
# First index is in range, second index is not in step direction.
>>> str1[14:1]
''
>>> str1[14:-16]
''
>>> str1[-1:-16]
''
Reply


Messages In This Thread
String slicing - by divyansh - May-30-2020, 09:30 PM
RE: String slicing - by BitPythoner - May-30-2020, 09:44 PM
RE: String slicing - by divyansh - May-30-2020, 10:04 PM
RE: String slicing - by menator01 - May-30-2020, 10:13 PM
RE: String slicing - by bowlofred - May-30-2020, 10:17 PM
RE: String slicing - by ibutun - May-31-2020, 12:55 AM
RE: String slicing - by pyzyx3qwerty - May-31-2020, 06:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 576 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,515 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  String slicing and loop iteration divyansh 9 4,699 Jun-07-2020, 10:29 PM
Last Post: divyansh
  string slicing help oli_action 2 2,180 Mar-22-2020, 09:57 AM
Last Post: oli_action
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,770 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing default value Uchikago 1 2,807 Jul-01-2019, 11:19 AM
Last Post: perfringo
  string slicing Uchikago 2 2,379 Jun-28-2019, 06:35 AM
Last Post: perfringo
  String slicing in python from reverse ift38375 1 2,388 Apr-29-2019, 06:58 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 2,248 Mar-21-2019, 09:33 PM
Last Post: nilamo
  String slicing problem Ollew 4 2,781 Sep-08-2018, 08:07 PM
Last Post: Ollew

Forum Jump:

User Panel Messages

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