James W.
—You’d like to get a subset of a string or array with a single expression. For example, you have the following variables storing some data that you need to manipulate:
hello_world_string = "Hello World!" num_string = "012345"
Since in Python, strings are arrays, we can use the same syntax for both object types.
Slicing segments an array and returns that segment.
In the example below, a string is segmented to return a single word from within the string:
hello_world_string = "Hello World!" world = hello_world_string[7:12] print(world)
Output:
World
Slicing requires a start and a stop:
array[start:stop]
The start is the index of the array that you would like the slice to begin with, the stop is the index of the array you would like to stop at.
The value of the array at the stop index is not included in the slice.
num_string = "012345" zero_to_four = num_string[0:5] print(zero_to_four)
Output:
01234
If an index is not provided for start, the slice will begin at the start of the array.
If an index is not provided for stop, the slice will end after the end of the array.
# Returns entire array array[:]
For example:
num_string = "012345" zero_to_five = num_string[:] print(zero_to_five)
Output:
012345
Slicing can also use a third value called step:
array[start:stop:step]
The step designates the manner in which the slice can walk through the array.
Assigning a step of 2
will include values from the array every two “steps”.
In the example below, a string is sliced starting at index 2, stopping when the array is complete, and taking the values at every second step.
num_string = "0123456" even_nums = num_string[2::2] print(even_nums)
Output:
246
Slicing can use negative indexing. With negative indexing, the last value in an array is -1, the second last is -2, and so on.
For example:
num_string = "012345" one_two_three = num_string[-5:-2] print(one_two_three)
Output:
123
Use negative indexing to step through an array in reverse.
For example:
num_string = "012345" reverse_string = num_string[::-1] print(reverse_string)
Output:
543210
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.