
Table of Contents
Indexing
To access the individual elements in a sequential data types like a string, list, or tuple using their index position. In Python, the indexing number starts from 0 for the first element.
- Indexing refers to the position of values or data stored in sequence objects, accessed using square brackets [].
- Indexes must be integers and within the valid range; otherwise, an error will occur.
Two types of indexing
- Positive Indexing: Starts from 0, going from the leftmost to the rightmost element.
- Negative Indexing: Starts from -1, going from the rightmost to the leftmost element.

Syntax: sequence[index_number]
Example:
my_list = [10, 20, 30, 40, 50]
print(my_list[0])
print(my_list[2])
print(my_list[-1])
print(my_list[-3])
Output:
10 # positive indexing
30 # positive indexing
50 # negative indexing
30 # negative indexing
Slicing
Allows you to access a portion of elements from a sequence data type by specifying a range of index positions or numbers.
Syntax: var_seq[start : stop : step]
- start: The starting index (inclusive).
- stop: The ending index (exclusive). (ending index position + 1)
- step: The interval between indices (optional, defaults to 1).
Note:
- If the starting or ending index is not specified, it defaults to the beginning or end of the string, respectively.
- The default step value is 1.
message = "Python Concept Slicing"
message[:] → Accesses the entire string from the start to the end.
message[::] → Accesses the entire string from the start to the end.
message[1::] → Accesses the string from the 1st position to the end.
message[3::] → Accesses the string from the 3rd position to the end.
message[:5:] → Accesses the string from the start to the 4th position, stepping by 1.
message[2:8:2] → Accesses the string from the 2nd to the 7th position, stepping by 2.
Example:
# List Datatype
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4])
print(my_list[:3])
print(my_list[2:])
print(my_list[::2])
print(my_list[::-1])
Output:
[20, 30, 40] # from index 1 to 3
[10, 20, 30] # from start to index 2
[30, 40, 50] # from index 2 to end
[10, 30, 50] # every second element
[50, 40, 30, 20, 10] # reversed list
# String Datatype
s = "Python Programming"
# Slicing
print(s[0:6]) # Output: 'Python' (from index 0 to 5)
print(s[7:]) # Output: 'Programming' (from index 7 to end)
print(s[:6]) # Output: 'Python' (from start to index 5)
print(s[::2]) # Output: 'Pto rgamn' (every second character)
print(s[::-1]) # Output: 'gnimmargorP nohtyP' (reversed string)
Output:
'Python' # from index 0 to 5
'Programming' # from index 7 to end
'Python' # from start to index 5
'Pto rgamn' # every second character
'gnimmargorP nohtyP' # reversed string
Characteristics
- Immutable Sequences: Strings and tuples are immutable; you cannot modify them using indexing or slicing. You can only create new sequences.
- Flexible Ranges: Skipping the start or stop in slicing allows you to access elements from the beginning or up to the end of the sequence.
- Step Value: This can be positive or negative, which is useful for reversing sequences.
Conclusion
Indexing and slicing are powerful tools in Python for accessing and manipulating sequences. They provide a straightforward way to extract individual elements or portions of sequences, whether it’s for processing data or simply retrieving specific parts.
Knowledge Check
Check out our Trending Courses Demo Playlist
| Data Analytics with Power Bi and Fabric |
| Could Data Engineer |
| Data Analytics With Power Bi Fabic |
| AWS Data Engineering with Snowflake |
| Azure Data Engineering |
| Azure & Fabric for Power bi |
| Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268


Most Commented