Skip to content

Python String Slicing

String slicing is a way to extract a part of a string. In Python, you can slice strings using a range of indices. While slicing strings, you can also specify the step size, which makes it possible to slice only every other character, or every third character in a string.

In this tutorial, we’ll learn how to use string slicing in Python. We’ll also learn how to reverse a string using slicing, and how to use negative indices to slice a string.

In Python, strings are ordered sequences of character data, and thus can be indexed in this way. You can access individual characters of a string using indexing and a range of characters using slicing. Strings are immutable data types, which means that once a string is created, you can’t modify it.

WordsHelloWorld
Indices012345678910

Let’s look at some examples of indexing strings in Python.

strings.py
# define a string
string = "Hello World"
print(string[0])
print(string[1])

Output:

command
C:\Users\Your Name> python strings.py
H
e

In the above example, we have indexed the string at index 0. This means that we have accessed the first character of the string.

In Python, you can slice strings using a range of indices. The general syntax for slicing a string is as follows:

Here, start and stop are the indices of the slice, and step is the step size. The start index is inclusive, and the stop index is exclusive. The step size defaults to 1 if not provided.

Let’s look at some examples of slicing strings in Python.

strings.py
# define a string
string = "Hello World"
print(string[0:5])

Output:

command
C:\Users\Your Name> python strings.py
Hello

In the above example, we have sliced the string from index 0 to index 5. Since the start index is inclusive, the character at index 0 is included in the slice. Since the stop index is exclusive, the character at index 5 is not included in the slice.

strings.py
# define a string
string = "Hello World"
print(string[0:5:2])

Output:

command
C:\Users\Your Name> python strings.py
Hlo

In the above example, we have sliced the string from index 0 to index 5 with a step size of 2. This means that we have sliced the string from index 0 to index 5, but only every other character.

Accessing Characters using Negative Indices

Section titled “Accessing Characters using Negative Indices”

In Python, you can also access characters of a string using negative indices. Negative indices start from the end of the string, and the index of the last character is -1.

WordsHelloWorld
Indices-11-10-9-8-7-6-5-4-3-2-1

Let’s look at some examples of accessing characters of a string using negative indices.

Example 1: Accessing a Character using a Negative Index

Section titled “Example 1: Accessing a Character using a Negative Index”
strings.py
# define a string
string = "Hello World"
print(string[-1])
print(string[-2])

Output:

command
C:\Users\Your Name> python strings.py
d
l
strings.py
# define a string
string = "Hello World"
print(string[-5:-2])

Output:

command
C:\Users\Your Name> python strings.py
Wor

In the above example, we have sliced the string from index -5 to index -2. Since the start index is inclusive, the character at index -5 is included in the slice. Since the stop index is exclusive, the character at index -2 is not included in the slice.

strings.py
# define a string
string = "Hello World"
print(string[:5])

Output:

command
C:\Users\Your Name> python strings.py
Hello

In the above example, we have sliced the string from the start to index 5. Since the start index is inclusive, the character at index 0 is included in the slice. Since the stop index is exclusive, the character at index 5 is not included in the slice.

strings.py
# define a string
string = "Hello World"
print(string[6:])

Output:

command
C:\Users\Your Name> python strings.py
World

In the above example, we have sliced the string from index 6 to the end. Since the start index is inclusive, the character at index 6 is included in the slice. Since the stop index is exclusive, the character at the end is also included in the slice.

strings.py
# define a string
string = "Hello World"
print(string[::-1])

Output:

command
C:\Users\Your Name> python strings.py
dlroW olleH

In the above example, we have sliced the string from the start to the end with a step size of -1. This means that we have sliced the string from the start to the end, but in reverse order.

s[start:stop] keeps the characters from index start up to but not including stop. Each character has a positive index (from the left) and a negative one (from the right). Watch the slice window move and the result update:

sketch s[start:stop] selects a range p5.js
Slicing keeps characters from start up to (but not including) stop. Positive indices count from the left, negative from the right.

In this tutorial, we learned how to slice strings in Python. We also learned how to reverse a string using slicing, and how to use negative indices to slice a string. Now you can use string slicing to extract parts of a string in Python. For more information, check out the official documentation for string slicing. For more tutroials, visit Python Central Hub.

Section titled “In this tutorial, we learned how to slice strings in Python. We also learned how to reverse a string using slicing, and how to use negative indices to slice a string. Now you can use string slicing to extract parts of a string in Python. For more information, check out the official documentation for string slicing. For more tutroials, visit Python Central Hub.”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading