Probelm in displaying char. indexwise - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Probelm in displaying char. indexwise (/thread-1992.html) |
Probelm in displaying char. indexwise - ShailendraRaghunathPhadke - Feb-09-2017 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 RE: Probelm in displaying char. indexwise - wavic - Feb-09-2017 In [1:3] 3 is like a frame. It's not included in the slice. RE: Probelm in displaying char. indexwise - buran - Feb-09-2017 Slicing: some_iterable[start:stop:step]stop is not included, so [1:3] would start at 1 and up to, but not including 3 RE: Probelm in displaying char. indexwise - sparkz_alot - Feb-09-2017 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' >>> RE: Probelm in displaying char. indexwise - ShailendraRaghunathPhadke - Feb-09-2017 (Feb-09-2017, 02:23 PM)buran Wrote: Slicing: OK Buran I understand your explanation. Thanks for reply. RE: Probelm in displaying char. indexwise - Ofnuts - Feb-09-2017 (Feb-09-2017, 02:33 PM)ShailendraRaghunathPhadke Wrote:(Feb-09-2017, 02:23 PM)buran Wrote: Slicing: 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 7So, here, [1:3] brackets 'el'
|