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


Messages In This Thread
Python Substring - by muzikman - Nov-20-2020, 01:32 PM
RE: Python Substring - by perfringo - Nov-20-2020, 02:21 PM
RE: Python Substring - by deanhystad - Nov-20-2020, 04:07 PM
RE: Python Substring - by muzikman - Dec-01-2020, 02:45 PM
RE: Python Substring - by deanhystad - Dec-01-2020, 03:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 549 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Python implementation of Longest Common Substring problem Bolt 0 566 Sep-17-2023, 08:31 PM
Last Post: Bolt
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,074 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 7,953 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,444 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Substring Counting shelbyahn 4 6,153 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Removing items from list if containing a substring pythonnewbie138 2 2,215 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,364 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,611 Sep-23-2019, 05:34 PM
Last Post: ichabod801
  Substring extraction nevendary 6 3,966 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