Python Programming question - strings and indexing?
-
How do I index a list of an unknown length starting at a certain point, lets say the 5th character (index 4) and splice the list starting from that index to the final character in the list? example question: Write an expression whose value is the str consisting of all the characters (starting with the sixth) of the str associated with s . or... Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders .
-
Answer:
say, you want to get the substring from a'th to b'th s[a:b] or if you want from the beginning to b'th: s[:b] or from a'th to the end: s[a:] this is called slicing and it also works with any sequence (e.g. list, tuple). FYI, a[:] is a python idiom for creating a shallow copy of a list FYI, there is also the extended slicing, which adds a third argument explained here: http://www.python.org/doc/2.3.5/whatsnew/section-slices.html
Hayden at Yahoo! Answers Visit the source
Related Q & A:
- What are the common usage of python programming language?Best solution by quora.com
- Are strings garbage collected?Best solution by Stack Overflow
- Is zero-based indexing impossible?Best solution by Stack Overflow
- How to Convert a Procedural Programming into Object-Oriented Programming?Best solution by Stack Overflow
- Help on Lisp programming question.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.