Home
Search
Substring ExamplesTake slices on strings to get substrings. Slice syntax works as a substring method.
Python
This page was last reviewed on Jan 7, 2023.
Substring. In Python we use slice syntax to get parts of existing strings—we can extract words from strings. We specify starts and ends.
Shows a slice
With indexes and slices, we have one, two or three parts. We use a start index and an end index. The final value is a step—an amount to skip between.
First example. This code slices strings in many ways. The loop iterates through a range of the string. We test several syntax forms.
Part A We start with the simplest possible example. We get a substring starting at index 1, and ending at index 3.
Part B We get more substrings in a for-loop. We try the start, end, and skip values.
Info The item access, s[n], requires only one number. This is an index access. It can be thought of as a 1-element slice.
Shows a slice
# Part A: the simplest possible example. animal = "bird" print(animal[1:3]) print() # Part B: more complex syntax for substrings. 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])
ir 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
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)
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).
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)
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).
string = "abcdefgh" # Three characters starting at index 2. offset = 2 length = 3 sliced = string[offset:offset+length] print(sliced)
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.
value = "rainy day" # Omitting the start means start at 0. test = value[:2] print("OMIT START:", test) test = value[0:2] print("START AT 0:", test)
OMIT START: ra START AT 0: ra
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.
value = "apple" # Get last two characters. end = value[-2:] print(end)
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.
Error
value = "dotperls" value[3] = " net " # Does not work. print(value)
Traceback (most recent call last): File "...\file.py", line 6, in <module> value[3:] = " net " TypeError: 'str' object does not support item assignment
First words. Here we use a string slice to get the first words in a string. The first_words method counts spaces. When it reaches the requested number, it returns a slice of the string.
Result When we use first_words, we extract the first words separated by spaces. A slice is used to take the substring.
def first_words(input, words): for i in range(0, len(input)): # Count spaces in the string. if input[i] == ' ': words -= 1 if words == 0: # Return the slice up to this point. return input[0:i] return "" # Test our method. test = "Stately, plump Buck Mulligan came from the stairhead." result = first_words(test, 4) print(result)
Stately, plump Buck Mulligan
All possible substrings. Suppose we want to get all possible substrings in a string. This can be used for some search algorithms—it lets us instantly test whether the substring is found.
Tip We can place all possible substrings in a dictionary, and then do constant-time lookups to search a string.
For We use the for-range loop in Python to iterate through all lengths and start indexes in the source string.
test = "abcdefghi" # Loop over possible lengths. for length in range(1, len(test)): # Loop over possible start indexes. for start in range(0, len(test) - length + 1): # Take substring and print it. part = test[start:length + start] print(part)
a b c d e f g h i ab bc cd de ef fg gh hi abc bcd cde def efg fgh ghi abcd bcde cdef defg efgh fghi abcde bcdef cdefg defgh efghi abcdef bcdefg cdefgh defghi abcdefg bcdefgh cdefghi abcdefgh bcdefghi
Right. The rightmost few characters of a string can be extracted. We use slice syntax for this too. But we begin with a negative index.
String Right
Truncate. A truncated string is the first part of a string. In Python we can specify a string slice with a starting index of 0. Or we can omit the 0.
Truncate String
Between. Sometimes we must use additional logic to get a slice. We can slice characters between two other strings with find and rfind.
Between, Before, After
Some notes. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 7, 2023 (simplify).
Home
Changes
© 2007-2023 Sam Allen.