String Slicing
In Python, you can also separate certain sections of the data if the date type is slicable.
Similar to indexing, we also use [ ] square brackets to slice sequences.
Slicing Format
Given a string value, we can slice it as following:
string_value[starting_index : ending_index : step_value]
Slicing generates a new string; therefore, slices can be set to a variable
The slice will start and include the value at
starting_indexThe slice will end at
ending_index, but not include the value at theending_indexThe
ending_indexcan be a value greater than the largest index possibleIf the
step_valueis not specified, it is set to:1If the slicing values are set to an impossible outcome, it will return an empty string:
''
# Example
'''
Looking at: 'Hello!'
| H | e | l | l | o | ! |
0 1 2 3 4 5 6
| H | e | l | l | o | !
-6 -5 -4 -3 -2 -1
'''
word = 'Hello!'
print('word:', word)
print('----------------------')
print('word[0:6]:', word[0:6])
print('word[0:5]:', word[0:5])
print('----------------------')
print('word[1:4]:', word[1:4])
print('word[:3]:', word[:3])
print('word[2:]:', word[2:])
print('word[:]:', word[:])
print('----------------------')
print('word[0:6:2]:', word[0:6:2])
print('word[::2]:', word[::2])
print('word[1:4:3]:', word[1:4:3])
print('word[::-1]:', word[::-1])
print('word[-5:-2]:', word[-5:-2])
print('word[6:0:-1]:', word[-5:-2])
print('word[-1:-6:-1]:', word[-1:-6:-1])
print('----------------------')
print('Some impossible slices and their results:')
print('word[6:0]:', word[6:0], '<< en empty string has been outputted.')
print('word[:20]:', word[:20], '<< There are no characters beyond the index of 6')
print('word[1:4:-1]', word[1:4:-1], '<< en empty string has been outputted.')Example: Iterate through a string backwards
Last updated