Register to get access to free programming courses with interactive exercises

String cuts Python Basics

When we work with strings in programming, we regularly have to extract parts of them.

For example, we need to find a smaller string inside a larger one. In this lesson, we'll look at how to do it.

Substrings and slices for strings

A substring is part of a string that needs to be found and extracted.

Suppose we have a date in this format: 12-08-2034. We need to extract a substring from it that only has the year.

If you think about it logically, you have to calculate the index from the first character of the year and extract the four characters. The indices in the string start with zero, so the first character of the year is available at index 6, and the last character is at index 9. Let's check it out:

value = '12-08-2034'

print(value[6])  # => 2
print(value[9])  # => 4

Now we know these indices, we can use slices and get the desired substring:

value = '12-08-2034'

year = value[6:10]
print(year)  # => 2034

In Python, string slices are a mechanism by which we extract a substring according to specified parameters. In the example above, we took a substring from index 6 up to but not including index 10, meaning from 6 to 9 inclusive. The formula looks like this:

str[start index:end index]

# A couple of examples
value = '01-12-9873'

# A string slice is always a string
# Even if there was a number inside the string
value[1:2]  # '1'
value[3:5]  # '12'

Slices are a tool with many variations. For example, if we don't specify a second boundary, we extract all the characters up to the end of the string. It applies to the first boundary, meaning the beginning of the line:

value = 'Hexlet'
value[3:]  # 'let'
value[:3]  # 'Hex'

You can even specify negative indices. In this case, it starts from the opposite side:

value = 'Hexlet'
# The right boundary is negative
# We count -1 from the end of the line
value[3:-1]  # 'le'
# The left boundary is negative
# We count -5 from the end of the line
value[-5:3]  # 'ex'

Slices have two mandatory parameters, but sometimes we can use a third one.

Extraction steps

Slices have a third optional parameter, extraction step. It's 1 by default, but we can change it:

value = 'Hexlet'
value[1:5:2]  # el
# 1:5 is 'exle'
# Step two is every second character, meaning 'e' and 'l'

All of these can be combined with open boundaries, in other words, without setting a beginning or end:

value = 'Hexlet'
value[:5:2]  # 'Hxe'
value[1::2]  # 'elt'

The step can be negative. In this case, we work with it from the end. From this comes the most popular way to use the extraction step; to reverse the string:

value = 'Hexlet'
# Skip both boundaries
value[::-1]  # 'telxeH'

If we use a negative step and extract the slice elements in reverse order, we should also specify the slice boundaries in reverse order. We indicate at first the right slice boundary and then the left one:

value = 'Hexlet'
# Python does not include the character at index one in the substring
value[4:1:-1]  # 'elx'

The slices can be specified using variables as well as numbers:

value = 'Hexlet'
start = 1
end = 5
value[start:end]  # 'exle'

As you can see, slices do a lot. Don't worry if you don't remember all these combinations right now. Later, you'll learn how to use them without looking at the documentation.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY