Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Slicing
#1
explain this to me ?
astring = "Hello world!"
print(astring[3:7])
print(astring[3:7:1])
Reply
#2
that is called slicing. you can slice any iterable - string, list, tuple, etc. the general syntax is

someiterable[start:stop:step]
note that index is 0-based
Reply
#3
I am answering this, partly to also understand the question for my own use.

The "anomoly" that I see, coming from a non Python background, is:

The [start] 3 is counting from a position of 0 rather than 1

But the [stop] 7 is counting from 1 rather than 0

The third element is as said by @buran is a [step] - so in this situation may not make any difference, as 1 may be the normal step. If you changed this to -1 you would be reversing the string as you are stepping backwards.

Do others agree with the starting/counting positions [start from 0] and [stop from 1] or have I misunderstood?

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#4
(May-04-2017, 10:45 AM)Bass Wrote: The [start] 3 is counting from a position of 0 rather than 1

But the [stop] 7 is counting from 1 rather than 0

In the programming indexes are almost all of the time 0-based.
stop is counting from 0 too, however it's starting from (including) start index up to (but not including) stop index.
Reply
#5
Also note that you can have negative indexes

>>> 'somestring'[-5:-1]
'trin'
in this case indexes go from right to left.
Reply
#6
Got you. Understand now.

Thanks


Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#7
Yo can also use None as either start or stop indices - as start it means from 0, as end - till end.
In [95]: l = list(range(3))

In [96]: l[None:]
Out[96]: [0, 1, 2]

In [97]: l[:None]
Out[97]: [0, 1, 2]
The latter is especially useful when using variable as stop index - l[0:] is ugly, but legal; however, there's no other way for stop index to mark the end of the list
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
Actually, I think I have never seen None used as index. i.e. I would say that actually it's legal , but ugly :-)
Reply
#9
(May-04-2017, 11:20 AM)buran Wrote: Actually, I think I have never seen None used as index. i.e. I would say that actually it's legal , but ugly :-)
Actually, for a slice till the end you may use any index that is larger than or equal to list/string lenth - but that in case that you know length in advance.

And it's not intended for direct use in code, actually that would be plain stupid Snooty - that was just a demonstration.
It is useful technique when stop index is passed as a variable...
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#10
Aha, OK, obviously I misunderstood that part:

(May-04-2017, 11:16 AM)volcano63 Wrote: The latter is especially useful when using variable as stop index - l[0:] is ugly, but legal;
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 534 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  String slicing and loop iteration divyansh 9 4,617 Jun-07-2020, 10:29 PM
Last Post: divyansh
  String slicing divyansh 6 3,281 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty
  string slicing help oli_action 2 2,150 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,719 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing default value Uchikago 1 2,783 Jul-01-2019, 11:19 AM
Last Post: perfringo
  string slicing Uchikago 2 2,331 Jun-28-2019, 06:35 AM
Last Post: perfringo
  String slicing in python from reverse ift38375 1 2,358 Apr-29-2019, 06:58 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 2,225 Mar-21-2019, 09:33 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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