Python Central HubSign in

Python Programming / Python Strings

Python String Slicing

Learn to slice strings in Python using a step-by-step tutorial. Learn to reverse a string using slicing, and how to use negative indices to slice a string.

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.

Array Indexing in Python

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.

Example 1: Indexing a String

Python
# define a string
string = "Hello World"
print(string[0])
print(string[1])

Output:

cmd
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.

Slicing Strings in Python

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

string[start:stop:step]

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.

Example 1: Slicing a String

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

Output:

cmd
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.

Slicing a String with a Step Size

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

Output:

cmd
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

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

Python
# define a string
string = "Hello World"
print(string[-1])
print(string[-2])

Output:

cmd
C:\Users\Your Name> python strings.py
d
l

Slicing a String with Negative Indices

Python
# define a string
string = "Hello World"
print(string[-5:-2])

Output:

cmd
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.

Slice from the Start

Python
# define a string
string = "Hello World"
print(string[:5])

Output:

cmd
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.

Slice to the End

Python
# define a string
string = "Hello World"
print(string[6:])

Output:

cmd
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.

Reverse a String using Slicing

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

Output:

cmd
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.

Visualize it

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.

Conclusion

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.


Try it: String Slicing Exercises

Exercise 1 – Basic Slicing

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Slice with Step

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Reverse a String

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did