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.
Array Indexing in Python
Section titled “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.
| Words | H | e | l | l | o | W | o | r | l | d | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Indices | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Let’s look at some examples of indexing strings in Python.
Example 1: Indexing a String
Section titled “Example 1: Indexing a String”# define a string
string = "Hello World"
print(string[0])
print(string[1])Output:
C:\Users\Your Name> python strings.py
H
eIn 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
Section titled “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]
Section titled “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
Section titled “Example 1: Slicing a String”# define a string
string = "Hello World"
print(string[0:5])Output:
C:\Users\Your Name> python strings.py
HelloIn 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
Section titled “Slicing a String with a Step Size”# define a string
string = "Hello World"
print(string[0:5:2])Output:
C:\Users\Your Name> python strings.py
HloIn 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.
| Words | H | e | l | l | o | W | o | r | l | d | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 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”# define a string
string = "Hello World"
print(string[-1])
print(string[-2])Output:
C:\Users\Your Name> python strings.py
d
lSlicing a String with Negative Indices
Section titled “Slicing a String with Negative Indices”# define a string
string = "Hello World"
print(string[-5:-2])Output:
C:\Users\Your Name> python strings.py
WorIn 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
Section titled “Slice from the Start”# define a string
string = "Hello World"
print(string[:5])Output:
C:\Users\Your Name> python strings.py
HelloIn 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
Section titled “Slice to the End”# define a string
string = "Hello World"
print(string[6:])Output:
C:\Users\Your Name> python strings.py
WorldIn 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
Section titled “Reverse a String using Slicing”# define a string
string = "Hello World"
print(string[::-1])Output:
C:\Users\Your Name> python strings.py
dlroW olleHIn 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
Section titled “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:
Conclusion
Section titled “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.
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.”Try it: String Slicing Exercises
Section titled “Try it: String Slicing Exercises”Exercise 1 – Basic Slicing
Section titled “Exercise 1 – Basic Slicing”Exercise 2 – Slice with Step
Section titled “Exercise 2 – Slice with Step”Exercise 3 – Reverse a String
Section titled “Exercise 3 – Reverse a String”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading