Code viewer for World: Strings: Indexing and Slicing
"""
Topic: Strings: Indexing and Slicing
Purpose: Explore how to access parts of a string in Python
Output: Shows individual characters and slices of a string
"""

# -----------------------------
# 1. Example string
# -----------------------------
text = "Python"

# -----------------------------
# 2. String indexing
# -----------------------------
# Each character in a string has a position called an index.
# Python starts counting from 0.

print(f"Full text: {text}")
print(f"First character (index 0): {text[0]}")  # P
print(f"Second character (index 1): {text[1]}") # y
print(f"Last character (index -1): {text[-1]}") # n (negative index counts from the end)

# -----------------------------
# 3. String slicing
# -----------------------------
# Slicing lets you get a part of the string using [start:stop] syntax.
# The slice includes the start index but excludes the stop index.

print(f"First three characters: {text[0:3]}")  # Pyt
print(f"Characters from index 2 to 4: {text[2:5]}") # tho
print(f"From start to index 3: {text[:4]}")  # Pyth
print(f"From index 3 to end: {text[3:]}")   # hon
print(f"Whole string using slicing: {text[:]}") # Python

# -----------------------------
# 4. Notes
# -----------------------------
# 1. Indexing starts at 0 for the first character.
# 2. Negative indices count from the end (-1 is the last character).
# 3. Slicing is [start:stop] and does not include the stop index.
# 4. Slicing is useful to extract parts of a string easily.