A Python substring is a portion of text taken from a string. You can extract a substring in Python using slicing, with the format: YourString[StartingIndex:StoppingIndex:CharactersToSkip].
Often, programmers have data that they want to split up into different parts. For example, a developer may have the full name of a user, and only need to get their first name. In this case, the developer would want to split up the name into two parts: forename and surname.
How do you accomplish this in the Python programming language? That’s where string slicing comes into play. Using a technique called slicing, we can get a specific part of a string, which then becomes a substring. In this tutorial, we are going to break down how substrings work in Python, and how you can use slicing to create your own.
String Indexes: A Refresher
Before we go on to discuss slicing and the slice syntax, we must first look at how Python strings are indexed.
In Python, a string is a sequence of one or more characters that could include numbers, spaces, letters, or symbols. You can access parts of a string in the same way that you would with other sequences, like a list.
Each character in a string has its own index value, which states the location of the character in the string. These numbers start with the index number 0. For example, here is how the string Python Substrings would be represented:
The first character in our string (P), has the index value of 0. The ! character has an index value of 16.
Because each character has its own index number, we are able to read and change individual characters in our strings.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Python String Substring
Let’s say that we want to get the first three characters from a string—how do we do it? Or what if we want to retrieve the last two characters in a string?
We can use a technique called slicing to retrieve particular pieces of data from a string. This is where we specify sequence of characters from a certain string that we want to retrieve. We can use slices to get any data we want from a string. The syntax we use is:
There are three components to a slice:
- start: The index position at which our slice should begin. This is 0 by default.
- end: The position at which the slice ends. The character at the end position is not included in the slice. By default, end is the length of the string.
- step: Instructs the slice to ignore every Nth character. N is equal to the step you specify. step is equal to 1 by default.
Substring Python: Examples
Retrieve the First Three Characters
We’re going to retrieve the first three characters from a string. To do so, we’ll use slicing syntax:
ourString = "Python Substring!" print(ourString[0:3])
The output for our code is:
Leave Out the Start Number
We can also leave out the first number to get all characters up to a certain value in the text. This is convenient if the text we need to get is at the start of a string.
We can use this technique to retrieve the first three characters from a string without having to specify 0 as our start number:
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
ourString = "Python Substring!" print(ourString[:3])
Our code returns the same output as our earlier example. We have not specified a start value. This is because start is 0 by default. We want to retrieve characters from the start of our string so this syntax works.
Retrieve the Last Two Characters
We can retrieve the last five characters of a string by using a negative index value and a start number:
value = "Python" print(value[-2:])
Our code returns: on. We have only specified one value: -2. This is our start number. -2 tells our slice to start retrieving characters from the second to last index position in our string.
We’ve prepared a few other examples to help you learn how to use the slicing method.
Retrieve the Last Character in a String
value = "Python" print(value[-1]) Output: n
We have only specified a start value in this example. There is no end value. This means our string will only retrieve the last character.
Retrieve Characters in the Range of 2 and 3
value = "Python" print(value[2:3]) Output: t
Our start value is 2 and our end value is 3. All the characters in the range of 2 and 3 are returned. This excludes character 3 itself, because the end value is exclusive.
Skip Every Second Character
value = "Python" print(value[::2]) Output: Pto
We have used two colons to tell Python that we want to use the default start and end parameters. We have specified 2 as our step. This means that our slice will retrieve every second chracter from our string.
View the Repl.it from this tutorial:
A substring is part of a larger string. To retrieve a substring, you can use the slicing syntax. Slicing lets you define a range of characters to retrieve. You can use an optional third parameter to skip over particular characters in a range.
Learn more about the Python language in our learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot