Python Slicing provide a concise way to create sub list, sub string from the existing list and strings respectively without using loop statement or if clauses. Slicing works in similar way for both Strings & Lists.
Slicing statement must have 2 paramater separated by colon to specify start & end position, and 3rd optional parameter also separated by colon to specify the increment.
Syntax: list_or_string_object[start_position : end_position : step]
a_str = 'DBMSTUTORIALS'
print(a_str[0:4])
Output: DBMS
a_str = 'DBMSTUTORIALS'
print(a_str[4:])
Output: TUTORIALS
a_str = 'DBMSTUTORIALS'
print(a_str[:])
Output: DBMSTUTORIALS
a_str = 'DBMSTUTORIALS'
print(a_str[0:-1:2])
Output: DMTTRA
a_str = 'DBMSTUTORIALS'
print(a_str[::-1])
Output: SLAIROTUTSMBD
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
print(a_list[0:4])
Output: ['D', 'B', 'M', 'S']
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
print(a_list[4:])
Output: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
print(a_list[:])
Output: ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
print(a_list[4:-1:2])
Output: ['T', 'T', 'R', 'A']
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
print(a_list[::-1])
Output: ['S', 'L', 'A', 'I', 'R', 'O', 'T', 'U', 'T', 'S', 'M', 'B', 'D']