Python - Slicing Strings

In Python, you can extract a range of characters from a string using slicing. The slicing syntax involves specifying a start index and an end index, separated by a colon, within square brackets, like this:

string[start:end]

Here's how it works:

Example: Getting a Range of Characters

Let's say we have the string "Hello, World!" as b. To get the characters from position 2 to position 5 (not including 5), you can use slicing like this:

b = "Hello, World!"
print(b[2:5])  # This will print "llo"

Remember that string indices start at 0. So, the first character, "H," is at position 0.

Slice From the Start

If you omit the start index, the range will start from the beginning of the string. For example:

b = "Hello, World!"
print(b[:5])  # This will print "Hello"

Slice To the End

If you leave out the end index, the range will continue to the end of the string:

b = "Hello, World!"
print(b[2:])  # This will print "llo, World!"

Negative Indexing

You can also use negative indexes to slice from the end of the string. For instance:

b = "Hello, World!"
print(b[-5:-2])  # This will print "orl"

With negative indexing, you specify the positions counting from the end of the string, starting with -1 for the last character.

Slicing is a powerful feature for working with strings in Python, as it allows you to extract and manipulate substrings easily.

Last updated