杰瑞科技汇

Python string index为何越界?

Of course! Let's break down string indexing in Python, from the basics to more advanced techniques.

Python string index为何越界?-图1
(图片来源网络,侵删)

What is String Indexing?

In Python, a string is an ordered sequence of characters. Indexing is the way to access individual characters from that sequence by their position (index).

Think of a string like a row lockers. Each locker holds one character, and each locker has a number on it (its index).


Basic Indexing (Positive Indexing)

Python, like most programming languages, starts counting indexes from 0, not 1.

  • The first character is at index 0.
  • The second character is at index 1.
  • The third character is at index 2.
  • ...and so on.

Syntax: my_string[index]

Python string index为何越界?-图2
(图片来源网络,侵删)

Example:

my_language = "Python"
# Accessing individual characters
first_char = my_language[0]  # 'P'
second_char = my_language[1]  # 'y'
third_char = my_language[2]  # 't'
print(f"The first character is: {first_char}")
print(f"The third character is: {third_char}")
# You can also use indexing in expressions
print(f"The fourth character is: {my_language[3]}") # 'h'

Important Rule: If you try to access an index that is out of range (i.e., the number is too large for the string's length), Python will raise an IndexError.

# This will cause an error
# print(my_language[6]) # IndexError: string index out of range
# The indexes for "Python" are 0, 1, 2, 3, 4, 5.

Negative Indexing

Python provides a convenient feature called negative indexing, which allows you to count backward from the end of the string.

  • The last character is at index -1.
  • The second-to-last character is at index -2.
  • The third-to-last character is at index -3.
  • ...and so on.

Syntax: my_string[-index]

Python string index为何越界?-图3
(图片来源网络,侵删)

Example:

my_language = "Python"
# Accessing from the end
last_char = my_language[-1]  # 'n'
second_last_char = my_language[-2]  # 'o'
third_last_char = my_language[-3]  # 'h'
print(f"The last character is: {last_char}")
print(f"The second-to-last character is: {second_last_char}")

Important Rule: Just like positive indexing, if you use a negative index that is too large in magnitude (e.g., -7 for "Python"), you will get an IndexError.

# This will cause an error
# print(my_language[-7]) # IndexError: string index out of range

Slicing (Getting a Substring)

Indexing gets you one character. Slicing gets you a sequence of characters—a substring.

The syntax is [start : stop : step].

  • start: The index where the slice begins (inclusive). If omitted, it defaults to 0.
  • stop: The index where the slice ends (exclusive). The slice goes up to, but does not include, this character. If omitted, it defaults to the end of the string.
  • step: The "stride" or how many characters to skip. If omitted, it defaults to 1.

Basic Slicing Examples:

my_language = "Python is awesome!"
# Get characters from index 0 up to (but not including) index 6
slice1 = my_language[0:6]
print(f"Slice [0:6]: '{slice1}'") # 'Python'
# Omitting the start (defaults to 0)
slice2 = my_language[:6]
print(f"Slice [:6]: '{slice2}'") # 'Python'
# Omitting the stop (defaults to the end)
slice3 = my_language[7:]
print(f"Slice [7:]: '{slice3}'") # 'is awesome!'
# Get characters from index 11 up to (but not including) index 18
slice4 = my_language[11:18]
print(f"Slice [11:18]: '{slice4}'") # 'awesome'

Slicing with a Step:

my_language = "Python is awesome!"
# Get every second character from the start to the end
slice5 = my_language[::2]
print(f"Slice [::2]: '{slice5}'") # 'Pto s wso!'
# Get a substring and reverse it (by using a step of -1)
# This is a very common Python idiom
reversed_string = my_language[::-1]
print(f"Reversed string: '{reversed_string}'") # '!emosewa si nohtyP'
# Get from index 5 to the end, stepping backwards by 2
slice6 = my_language[5::-2]
print(f"Slice [5::-2]: '{slice6}'") # 'oP'

Common Pitfalls and Key Concepts

Strings are Immutable

This is a crucial concept in Python. Immutability means that once a string is created, it cannot be changed.

You cannot use indexing to change a character.

my_language = "Python"
# This will cause a TypeError!
# my_language[0] = 'J' # TypeError: 'str' object does not support item assignment
# To "change" a string, you must create a new one.
new_language = 'J' + my_language[1:]
print(new_language) # 'Jython'

Iterating Over a String with a Loop

A very common use of indexing is in a for loop to iterate through each character.

my_language = "Python"
# Method 1: Direct iteration (most Pythonic)
print("Method 1 (Direct):")
for char in my_language:
    print(char)
# Method 2: Using index and range (more manual)
print("\nMethod 2 (with range and len):")
for i in range(len(my_language)):
    print(f"Index {i}: {my_language[i]}")

Summary Table

Concept Syntax Example Output
Positive Indexing string[index] "Hello"[1] 'e'
Negative Indexing string[-index] "Hello"[-2] 'l'
Slicing (Basic) string[start:stop] "Hello"[1:4] 'ell'
Slicing (to End) string[start:] "Hello"[2:] 'llo'
Slicing (from Start) string[:stop] "Hello"[:3] 'Hel'
Slicing (Full Copy) string[:] "Hello"[:] 'Hello'
Slicing (with Step) string[start:stop:step] "Hello"[::2] 'Hlo'
Reversing a String string[::-1] "Hello"[::-1] 'olleH'
分享:
扫描分享到社交APP
上一篇
下一篇