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
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Python implementation of Longest Common Substring problem Bolt 0 531 Sep-17-2023, 08:31 PM
Last Post: Bolt
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,015 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 7,847 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,370 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Substring Counting shelbyahn 4 6,070 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Removing items from list if containing a substring pythonnewbie138 2 2,151 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,319 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,561 Sep-23-2019, 05:34 PM
Last Post: ichabod801
  Substring extraction nevendary 6 3,894 Apr-24-2019, 05:41 AM
Last Post: nevendary

Forum Jump:

User Panel Messages

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