Python Forum
Probelm in displaying char. indexwise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Probelm in displaying char. indexwise
#1
Hi,
I have started python programming. And my question is related to string .

I wrote below statement
msg=  "Welcome"
print(msg)
I got theoutput as "Welcome" :correct

then,
print(msg[1])
I got the output as "e" : correct

then, I wrote
print(msg[1:3])
I got the output as "el" : I don't understand this

W e l c o m e
0  1 2 3 4 5 6

So, I am expecting the output as elc

then, why it is just showing me "el" ?

Please help me here
Thanks
Reply
#2
In [1:3] 3 is like a frame. It's not included in the slice.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Slicing:
some_iterable[start:stop:step]
stop is not included, so [1:3] would start at 1 and up to, but not including 3
Reply
#4
Basically, with your example:

print(msg[1:3])
you are saying msg[start with this character : up to but not including this character].

A bit confusing when you first start off. Even more so when you use a negative index:

>>> msg[-1]
'e'
>>> msg[-3]
'o'
>>>
but:

>>> msg[-3:-1]
'om'
>>>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(Feb-09-2017, 02:23 PM)buran Wrote: Slicing:
some_iterable[start:stop:step]
stop is not included, so [1:3] would start at 1 and up to, but not including 3

OK Buran I understand your explanation. Thanks for reply.
Reply
#6
(Feb-09-2017, 02:33 PM)ShailendraRaghunathPhadke Wrote:
(Feb-09-2017, 02:23 PM)buran Wrote: Slicing:
some_iterable[start:stop:step]
stop is not included, so [1:3] would start at 1 and up to, but not including 3

OK Buran I understand your explanation. Thanks for reply.

It can be easier to understand if you consider that the indices are really indices between the letters (or list elements)
 W e l c o m e
0 1 2 3 4 5 6 7
So, here, [1:3] brackets 'el'
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to replace on char with another in a string? korenron 3 2,294 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 2,486 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Sum char word dictionary RavCOder 3 2,293 Nov-08-2019, 01:32 PM
Last Post: perfringo
  The use of escape char \ hishamzero1 2 2,341 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  First non-repeating char and some errors. blackknite 1 2,238 Jan-06-2019, 02:19 PM
Last Post: stullis
  Probelm using Boolean input phrase.isupper() skrivver99 4 3,062 Nov-04-2018, 09:21 AM
Last Post: Larz60+
  json.dumps save some unicode chars as code, not char buran 1 2,881 Aug-02-2018, 04:02 PM
Last Post: buran
  pyserial char by char io fcagney 4 6,175 May-15-2018, 10:45 PM
Last Post: DeaD_EyE
  how to give your data column names based on char position whyme 7 4,352 Oct-27-2017, 06:45 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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