slice
A copy of a run of items from a string, list or tuple, written s[start:stop], which includes start and stops just before stop.
In more detail
Leave out start to begin at the beginning and stop to go to the end, so s[:] copies the whole thing. A third number is a step, as in s[::-1] to reverse. Slices are forgiving: bounds past the end are simply cut short instead of raising an error.
Example
"program"[3:7] # "gram"Related terms
- string index — The position number of a character in a string or an item in a list, counting from 0 at the left, used in square brackets as s[0].
- substring — A run of consecutive characters inside a longer string, such as "ssi" in "Mississippi".
- clone() — Making a separate, independent copy of an object, such as shape.clone() in graphics.py or a list copy with list(a).
Taught in
- String Indices — Hands-on Python: Complete Video Course & Book
- String Slices — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: Finding The Cues — Hands-on Python: Complete Video Course & Book
- Chapter Two In One Sitting — Hands-on Python: Complete Video Course & Book
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Strings: Escapes, Slices And Methods — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Characters As Numbers — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comparing, Slicing And Never Changing — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- String Methods The Exam Picks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Strings Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep