TheDeveloperBlog.com


Python Substring Examples: String Slice

Python Substring Examples: String Slice

String slice. Python has no substring method. Instead, we use slice syntax to get parts of existing strings. We specify starts and ends.


Some hints. With indexes and slices, we have one, two or three parts. We use a start index and an end index (not a length). The final value is a step—an amount to skip between.


An example. We begin with an example that slices a string within a for-loop. The loop iterates through a range of the string. We test several syntax forms.

Note: The item access, s[n], requires only one number. This is an index access. It can be thought of as a 1-element slice.

Tip: A skip value is rarely helpful on strings. It serves mostly to make programs harder to understand.

Based on:

Python 3

Python program that slices string

s = "abcdefghijklmnopqrs"

# Loop over some indexes.
for n in range(2, 4):
    # Print slices.
    print(n, s[n])
    print(n, s[n:n + 2])
    print(n, s[n:n + 3])
    print(n, s[n:n + 4:2])
    print(n, s[n:n + 6:2])

Output

2 c
2 cd
2 cde
2 ce
2 ceg
3 d
3 de
3 def
3 df
3 dfh

One character. Consider this program. It uses index accesses to get one-character substrings. We can test these substrings in an if-statement.

If
Python program that gets one-character substring

value = "web"

# Loop over every index in string.
for index in range(0, len(value)):

    # Get one-character substring.
    char = value[index]

    # Display substring.
    print(char)

    # Test substring.
    if char == "w":
        print(True)

Output

w
True
e
b

Lengths. We do not specify a length to get a substring with slice syntax. But when the start is equal to zero, the second argument will be the length.

Tip: The length of the slice is the difference between the two arguments (subtract the first from the second).

Python program that takes substrings

string = "abcdefgh"

# Two-character substring
two = string[0:2]

# Three-character substring
three = string[0:3]

# Four-character substring
four = string[0:4]

# Five-character substring
five = string[0:5]

print(two)
print(three)
print(four)
print(five)

Output

ab
abc
abcd
abcde

Offsets. This example shows the relation between the start and the end index. If you want a 3-character slice, add three to the start index (the offset).

Python program that uses offset

string = "abcdefgh"

# Three characters starting at index 2.
offset = 2
length = 3
sliced = string[offset:offset+length]
print(sliced)

Output

cde

Omit start. Sometimes there is no start index in a slice. This simply means the slice starts at zero. So the zero is implicit, assumed, and we can omit it.

Python program that omits start index

value = "google"

# Omitting the start means start at 0.
test1 = value[:2]
test2 = value[0:2]

print(test1)
print(test2)

Output

go
go

Negative values. A negative start begins the slice based on the last index (the end) not the first. Here we extract the last two characters of a string.

Python program that gets last two characters

value = "apple"

# Get last two characters.
end = value[-2:]
print(end)

Output

le

Item assignment error. A string cannot be mutated. So we cannot use slice syntax to assign into an existing string. Instead, we create a new string—we can concatenate slices.

TypeError: This error is used for many problems in the Python language. It indicates invalid type usage.

Errors
Python that causes error

value = "dotperls"
value[3] = " net " # Does not work.
print(value)

Output

Traceback (most recent call last):
  File "...\file.py", line 6, in <module>
    value[3:] = " net "
TypeError: 'str' object does not support item assignment

Substring method. Python offers full substring support. But you will find no "substring" method. With slice syntax, we specify substrings—this becomes natural after repetition.


Uniformity with arrays. In Python 3, arrays and strings are used in a uniform way. We index and slice them. We cannot however mutate string slices.


A review. With slices, we extract parts of strings. We can specify an optional start index and an optional last index (not a length). Offsets are useful.