Chapter 14: Step Argument

In this chapter, we will explore how to print characters from a string at specific intervals, also known as step-wise slicing. This allows us to extract characters in an alternating pattern or based on a defined step value. Additionally, we will learn how to reverse a string using a single argument.

 

Syntax: [start_index : end_index : step]

 

Note: If the starting and ending indices are not specified, the entire string is considered by default.

 

name = "Free Learning"
print(name[0:9:2]) # Alternate behaviour with start and ending parameter
Output: Fe er
print(name[0::2]) # Alternate behaviour without ending paramter
Output: Fe erig
print(name[::-1]) # Reverse String
Output: gninraeL eerF