Python offers many ways to substring a string. This is often called "slicing".

Here is the syntax:

string[start:end:step]

Where,

start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0.

end: The terminating index of the substring. The character at this index is not included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default.

step: Every "step" character after the current character to be included. The default value is 1. If step is not included, it is assumed to be equal to 1.

Basic Usage

string[start:end]: Get all characters from start to end - 1

string[:end]: Get all characters from the beginning of the string to end - 1

string[start:]: Get all characters from start to the end of the string

string[start:end:step]: Get all characters from start to end - 1, not including every step character

Examples

1. Get the first 5 characters of a string

string = "freeCodeCamp"
print(string[0:5])

Output:

> freeC

Note: print(string[:5]) returns the same result as print(string[0:5])

2. Get a substring 4 characters long, starting from the 3rd character of the string

string = "freeCodeCamp"
print(string[2:6])

Output:

> eeCo

3. Get the last character of the string

string = "freeCodeCamp"
print(string[-1])

Output:

> p

Notice that the start or end index can be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (from the right to left).

Index -1 represents the last character of the string, -2 represents the second to last character and so on.

4. Get the last 5 characters of a string

string = "freeCodeCamp"
print(string[-5:])

Output:

> eCamp

5. Get a substring which contains all characters except the last 4 characters and the 1st character

string = "freeCodeCamp"
print(string[1:-4])

Output:

> reeCode

6. Get every other character from a string

string = "freeCodeCamp"
print(string[::2])

Output:

> feCdCm