Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Substring
#1
Greetings,

I am new to the Python language; however, I have experience in other OOP languages. I have a question based on Python substring syntax.

The following code

my_string = "This is MY string!"
print(my_string[13:2:-1])
generates the return string
rts YM si s
I am a little confused at what this substring returns. I base it on the middle parameter '2', which should be 2-1 as I am told, as the following syntax would suggest:

[start:end-1:steps]
How come this does not seem to be working here? If the previous syntax is correct, it would have included the 'i' in the return string
rts YM si s
, instead it only includes the 's'.

My only reasoning behind this is, it works differently when you are counting down then when moving forward through the string?
Everything works as intended when I am counting up.

Any help would be greatly appreciated,
Regards,
Matt
Reply
#2
Question is long but answer is short: zero based indexing and end indice is 'first to be excluded'.

EDIT: for longer explanation refer to documenation: Documentation > The Python Tutorial > 3. An Informal Introduction to Python > 3.1.2. Strings

Quote:Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:
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.
Reply
#3
No, slices are messed up if you count up or down. But at least they are consistently messed up.
x = '0123456789'
print(x[2:6:1], x[6:2:-1])
Output:
2345 6543
Why don't these print out the same characters in a different order? Why doesn't the slice "x[2:6]" start at index 2 and end at index 6? Why is the "end" adjusted so abs(end-start) = len(x[start, end])? I think for loops are to blame.

I very much like that I can use "for _ in range(5):" to do something 5 times. It would be very confusing if I was forced to use "for _ in range(4)" and it looks awkward to use "for _ in range(1,5)" when I only care about how many times and not about particular index numbers. To me it is a forgivable offense that the "end" in "range(start, end, increment)" is adjusted by 1 to make the most common range usage look nice.
Reply
#4
So, the start is index based at zero. So,based on your example above, the start [2:6:1] either starts before index 2 or includes index 2? Either way, you get the same result.

End is not index based, correct? You just count normally from one? If we count the end by index, we end up including the '6'. So, we should just count the end starting from 1, correct?
Reply
#5
The index is always based at zero. The start is the start of the sequence and the end is not included in the sequence. This is true for counting up, down, by 1 or by many
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing items from list if containing a substring pythonnewbie138 2 656 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  ValueError: substring not found nby2001 1 2,438 Mar-08-2020, 09:00 PM
Last Post: deanhystad
  Substring and If then Condition to create column Chandan 2 784 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 2,781 Sep-23-2019, 05:34 PM
Last Post: ichabod801
  Substring extraction nevendary 6 1,655 Apr-24-2019, 05:41 AM
Last Post: nevendary
  substring between substrings Skaperen 5 1,786 Oct-27-2018, 08:45 PM
Last Post: ichabod801
  converting a substring of digits and keep what follows Skaperen 3 1,741 Apr-22-2018, 02:01 AM
Last Post: Skaperen
  Substring Counting shelbyahn 3 3,433 Jan-21-2018, 05:40 PM
Last Post: j.crater
  Ho to check if string contains substring from list Rius2 2 74,049 Sep-30-2017, 07:32 PM
Last Post: Rius2

Forum Jump:

User Panel Messages

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