How to Slice String in Python?


Slicing strings is a common task in programming. It allows you to manipulate strings by extracting a specific part of the string. When slicing in Python, it is important to specify the start and stop indices correctly so that you extract the correct part of the string.

How to Slice String in Python?

Slice String in Python

The slice will include characters from offset start to one before end:

  • [:] extracts the entire sequence from start to end.
  • [ start :] specifies from the start offset to the end.
  • [: end ] specifies from the beginning to the end offset minus 1.
  • [ start : end ] indicates from the start offset to the end offset minus 1.
  • [ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step.

To slice strings in Python, you need to follow the syntax below:

string[start:stop]
Code language: CSS (css)

For example, if you want to extract the first three characters of a string, you would do the following:

string[0:3]
Code language: CSS (css)

If you want to extract the last three characters of a string, you would do this:

string[-3:]
Code language: CSS (css)

However, if you want to extract a specific part of the middle of a string (say, characters 2-5), you would do this:

string[2:5]
Code language: CSS (css)

Example 1:

# String slicing String = 'PC-SAVVY' # Using indexing sequence print(String[:3])
Code language: Python (python)

Output

pc-

Example 2:

In this example, we’ll see how to go from an index of 1 to an index of 5 (stopping at 3-1=2), with a skip step of 2. It’s a good example of how Python can cut up a string character by character.

# String slicing String = 'PC-SAVVY' # Using indexing sequence print(String[1:5:2])
Code language: Python (python)

Output

cs

Example 3:

Prints string in reverse

# String slicing String = 'PC-SAVVY' # Prints string in reverse print(String[::-1])
Code language: Python (python)

Output

yvvas-cp

Example 5:

slice string python

Python will automatically adjust the indices if they are out of range. For example, if you have a string with 10 characters and try to extract characters 11-15, Python will return an empty string.

Extracting a substring from a larger string can be very useful when you are parsing data or working with text files.

For example, imagine that you have a list of email addresses and you want to extract only the domain names (everything after the @ sign). To do this, you can use string slicing.

Below is an example of how this would work:

my_emails = ["alice@gmail.com", "bob@yahoo.com", "carol@hotmail.com"] #for an email in my_emails print(email[email.index(‘@’)+1:]) # Prints ‘gmail.com’ on one line and ‘yahoo.com’ on another line # followed by ‘hotmail.com’ on another line
Code language: Python (python)

As you can see from this code example, using string slicing can be a very powerful tool for manipulating and extracting data from strings. String slicing is just one way to work with strings in Python—there are many other string methods that you can explore as well.

In this article, we learned how to slice strings in Python using the syntax string[start:stop]. We also learned how to use negative indices when slicing strings and how Python automatically adjusts out-of-range indices. Finally, we saw how powerful string slicing can be for extracting data from strings.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts