For AI agents: use /llms.txt for the Nakafa content index.
Indexing selects one element of a string or list by its position. For example, if letters = ["a", "b", "c"], then letters[1] returns "b".
Python starts its index numbers at . The first element has index , the second has index , and so on. Python also supports negative indices that count backward from the end.
In strings or lists, each character or element has two types of indices. Positive indices start from at the beginning, while negative indices start from at the end.
An index outside the valid range raises IndexError. This bounds check prevents Python sequences from returning an unrelated value.
A slice selects several elements from a string or list. You set a start index, a stop index, and, when needed, the distance between selected elements.
The syntax [i:j] uses i as the start index and j as the stop index. The result includes the element at i but excludes the element at j.
When the start index is greater than the stop index and the step is positive, the result is an empty string or list. A negative step selects items in the opposite direction.
The third parameter in [start:stop:step] sets the distance between selected elements. A step of 2, for example, selects one item and skips the next.
A negative step reverses the selection direction. With a step of , Python selects all elements from the end to the beginning.
The following rules determine the selected elements and the length of a slice.
For the simple case with the default positive step, s[i:j] has length . General slices may contain omitted, negative, or out-of-range bounds and a different step. Python first normalizes them against the sequence length. The exact count is len(range(*slice(i, j, step).indices(len(s)))).
Python clips slice bounds to the valid range of the string or list. Therefore, s[3:10] remains valid even when the stop index is greater than the length of s.
You can use range() to show which indices a slice selects. This model explains the result without making assumptions about how each Python type performs the operation internally.
Comparing the slice with its range() model shows how start, stop, and step work together, especially when the step is negative.
Omit the start or stop bound when the selection should extend to one end of the data.
You can leave the start or end parameter empty to take elements from the beginning or to the end of string/list.
These forms select a specific region or regularly spaced items from a larger dataset without a separate loop.
The examples below use indexing and slicing to reverse strings, format phone numbers, build initials, and check palindromes. The file extension example uses Path.suffix because extensions do not have a fixed length.
Use an index to select one item at a known position. Use a slice to select several adjacent or regularly spaced items. The form [::-1] reverses their order. For file extensions, avoid a fixed number of characters because extension lengths vary. Path.suffix reads the extension from the path structure.
Slice bounds are clipped to the sequence, so s[10:] on a shorter string returns "". Direct indexing such as s[10] still raises IndexError. Use a slice only when an empty result is acceptable.
Negative indices count from the end. Use s[-1] for the last element or s[-3:] for the last three elements. This form selects positions near the end during file processing, text parsing, or dataset work.
Published: . Updated: .
# Example of indexing on string
s = "Hello"
# Accessing characters with positive indices
print(s[0]) # Output: 'H'
print(s[4]) # Output: 'o'
# Accessing characters with negative indices
print(s[-1]) # Output: 'o' (last character)
print(s[-5]) # Output: 'H' (first character)
# Trying to access non-existent index
try:
print(s[5])
except IndexError:
print("IndexError: index out of range")# Example of slicing on string
s = "Hello"
# Slicing from index 1 to 3 (not including 3)
print(s[1:3]) # Output: 'el'
# Slicing from index 3 to end
print(s[3:]) # Output: 'lo'
# Slicing from start to index 3
print(s[:3]) # Output: 'Hel'
# Slicing with negative indices
print(s[-4:-2]) # Output: 'el'
print(s[-2:-4]) # Output: '' (empty string)# Example of slicing with stride
s = "Hello World!"
# Taking every second character
print(s[::2]) # Output: 'HloWrd'
# Taking every second character from index 1
print(s[1::2]) # Output: 'el ol!'
# Reversing string with negative stride
print(s[::-1]) # Output: '!dlroW olleH'
# Slicing with negative stride from specific index
print(s[-1:4:-1]) # Output: '!dlroW'# Calculating slicing result length
s = "Hello"
# Slicing s[1:4]
result = s[1:4]
print(f"Result: '{result}', Length: {len(result)}") # Output: 'ell', 3
# Proving that s[:i] + s[i:] == s
i = 2
left_part = s[:i]
right_part = s[i:]
print(f"'{left_part}' + '{right_part}' = '{left_part + right_part}'")
# Slicing outside bounds doesn't cause error
print(s[3:10]) # Output: 'lo'
print(s[10:]) # Output: '' (empty string)# Understanding slicing as loop
s = "Hello World!"
# s[1:8:2] equivalent to the following loop:
result_manual = ""
for index in range(1, 8, 2):
result_manual += s[index]
result_slicing = s[1:8:2]
print(f"Manual loop: '{result_manual}'") # Output: 'el o'
print(f"Slicing: '{result_slicing}'") # Output: 'el o'
print(f"Same? {result_manual == result_slicing}") # Output: True
# Example with negative stride
print("\nNegative stride:")
print(f"s[::-2] = '{s[::-2]}'") # Output: '!lo le'
# Equivalent manual loop for negative stride
result_negative = ""
for index in range(len(s)-1, -1, -2):
result_negative += s[index]
print(f"Manual: '{result_negative}'")# Useful slicing variations
s = "Hello"
# Taking first 3 characters
print(s[:3]) # Output: 'Hel'
# Taking characters from index 3 to end
print(s[3:]) # Output: 'lo'
# Taking all characters
print(s[:]) # Output: 'Hello'
# Practical example with list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Taking first 5 elements
first_five = numbers[:5]
print(f"First five: {first_five}")
# Taking last 5 elements
last_five = numbers[-5:]
print(f"Last five: {last_five}")
# Taking middle elements (skip first 2 and last 2)
middle = numbers[2:-2]
print(f"Middle part: {middle}")# Practical examples of indexing and slicing
# 1. Reversing name
first_name = "Ahmad"
last_name = "Wijaya"
reversed_name = (first_name + " " + last_name)[::-1]
print(f"Reversed name: {reversed_name}")
# 2. Getting a file extension safely
from pathlib import Path
filename = "important_document.pdf"
extension = Path(filename).suffix
print(f"Extension: {extension}")
# 3. Formatting phone number
number = "081234567890"
formatted = f"{number[:4]}-{number[4:8]}-{number[8:]}"
print(f"Formatted number: {formatted}")
# 4. Getting name initials
full_name = "Siti Nurhaliza Binti Ahmad"
words = full_name.split()
initials = ""
for word in words:
initials += word[0]
print(f"Initials: {initials}")
# 5. Palindrome checker
def is_palindrome(text):
cleaned = "".join(character for character in text.casefold() if character.isalnum())
return cleaned == cleaned[::-1]
print(f"'katak' palindrome? {is_palindrome('katak')}")
print(f"'kasur rusak' palindrome? {is_palindrome('kasur rusak')}")