Hello,
I'm not a newbie in python, but recently faced a problem in simple
expression:
some_string[i:j:k]
What does it mean? I believe this grammar ( http://docs.python.org/ref/
slicings.html) describes the syntax. But I can't grasp it.
Thanks! 6 11157
On Jul 22, 3:10*pm, konstantin <konstantin.seliva...@gmail.comwrote:
some_string[i:j:k]
What does it mean?
i = start position, j = end position, k = step size
>>s = "ABABABABABABAB" s[0:6:2]
'AAA'
>>s = "ABCABCABCABCABC" s[0:6:3]
'AA'
Hope this helps.
- alex23
On Jul 22, 9:18 am, alex23 <wuwe...@gmail.comwrote:
On Jul 22, 3:10 pm, konstantin <konstantin.seliva...@gmail.comwrote:
some_string[i:j:k]
What does it mean?
i = start position, j = end position, k = step size
>s = "ABABABABABABAB" s[0:6:2]
'AAA'
>s = "ABCABCABCABCABC" s[0:6:3]
'AA'
Hope this helps.
- alex23
Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>s = '123456789' s[::-2]
'97531'
but
>>s[:-1:-2]
''
though I expected something like '8642'
What did i missed?
konstantin wrote:
On Jul 22, 9:18 am, alex23 <wuwe...@gmail.comwrote:
>On Jul 22, 3:10 pm, konstantin <konstantin.seliva...@gmail.comwrote:
>>some_string[i:j:k] What does it mean?
i = start position, j = end position, k = step size
>>>>s = "ABABABABABABAB" s[0:6:2] >
'AAA'
>>>>s = "ABCABCABCABCABC" s[0:6:3] >
'AA'
Hope this helps.
- alex23
Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>>s = '123456789' s[::-2]
'97531'
but
>>>s[:-1:-2]
The slice s[:-1]
means start at zero and go to n-1(where n-len(s))
(it does not mean start at zero and go to -1)
So since the indexing is counting upward, the step size had better be
positive. Thus:
>>s = '123456789' s[:-1:2]
'1357'
>>>
Gary Herron
''
though I expected something like '8642'
What did i missed?
-- http://mail.python.org/mailman/listinfo/python-list
konstantin wrote:
>
Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>>s = '123456789' s[::-2]
'97531'
but
>>>s[:-1:-2]
''
though I expected something like '8642'
What did i missed?
--
You need to *start* at the second from last index:
s[-2::-2]
Regards,
John
konstantin wrote:
Hello,
I'm not a newbie in python, but recently faced a problem in simple
expression:
some_string[i:j:k]
What does it mean? I believe this grammar (http://docs.python.org/ref/
slicings.html) describes the syntax. But I can't grasp it.
When you post a link, please put it on one line by itself, like this: http://docs.python.org/ref/slicings.html
so it will be clickable as a whole by modern newsreaders.
Experimenting with bits of code in the interactive interpreter (or IDLE,
which is easier to cut from), like the responders did, is a great way to
learn. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Krakatioison |
last post by:
My sites navigation is like this:
http://www.newsbackup.com/index.php?n=000000000040900000
, depending on the variable "n" (which is always a number), it will take me
anywhere on the site......
|
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: John F Dutcher |
last post by:
I use code like the following to retrieve fields from a form:
recd =
recd.append(string.ljust(form.getfirst("lname",' '),15))
recd.append(string.ljust(form.getfirst("fname",' '),15))
etc.,...
|
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Andrew |
last post by:
I have written two classes : a String Class based on the book " C++ in
21 days " and a GenericIpClass listed below :
file GenericStringClass.h
// Generic String class
|
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: Badass Scotsman |
last post by:
Hello,
Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.
For example:
String1 = " That was...
|
by: morleyc |
last post by:
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.
Chris
|
by: ramu |
last post by:
Hi,
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove...
|
by: drjay1627 |
last post by:
hello,
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |