SLICING

In Python, slicing is a string operation used to extract a portion (substring) of a sequence such as a string, list, or tuple. Slicing uses the syntax [start:end], where start is the index of the first element to include and end is the index of the first element to exclude. Python also allows optional step values with [start:end:step], which specify the interval between elements. Negative indexes can be used to count from the end of the sequence, making it convenient to access elements relative to the sequence’s tail. Slicing always returns a new sequence and does not modify the original. This concept is similar to the substring operation in C or C++, though Python provides additional flexibility like negative indexing and step values.

#!/usr/bin/env python3

def main() -> None:
    months = ["January", "February", "March", "April", "May", "June", "July"];
    print(months[0:6]);
    print(months[1:5]);
    
if __name__ == "__main__":
    main();

Last updated