Python String Slice with Examples

To get a substring also known as a slice from a string in Python use str[] and sometimes str[slice()]. In this article, we will discuss two ways to return the substring from the string through a slicing approach. We will use slice() method and the array slice operator which will be used to slice the string. The string is a set of characters similar to C, C++, and other programming languages.

Things to know before slicing string:

  • The starting index position is 0. The ending index position is n-1. Here, n is the length of the string.
  • The negative index starts from the last character in the string i.e the last character index is -1.

1. Quick Examples of String slicing

Following are quick examples of how to slice a string in python.


# Consider the string
st1='Hello welcome to sparkby examples'
print("String: ",st1)

# Slice first 10 characters
print(st1[slice(10)])

# Slice from last 4th position to 10th position
print(st1[slice(-10,-3)])

# Slice from first character to 5th character
print(st1[slice(0,5)])

# Slice from first character to 5th character by skipping 3 characters
print(st1[slice(0,5,3)])

# Return the entire string using Array Slicing
print(st1[:])

# Slice from 7th character to 20th character
print(st1[6:20])

# Slice from 2nd character to 10th character with step as 3
print(st1[1:10:3])

# Slice from the first character to 20th character
print(st1[:20])

# Slice from 7th character  to end of the string.
print(st1[6:])

2. String Slicing using slice()

The slice() is a string inbuilt method available in python that is used to return the substring within the specified index range. It will take three parameters that refer to the index positions. Based on these indices, string slicing will happen. let’s look at its syntax and parameters.

2.1 Syntax of slice()


# Syntax of slice()
string[slice(start,stop,step)]

2.2 Parameters of slice()

  • start is the first parameter which refers to the starting index position. If it is specified, then the slicing will happen and the substring will be returned from this position.
  • stop is the second parameter. the slicing will end and the substring will be returned to this position.
  • step is an optional parameter that skips the number of characters within the slicing.

2.3 Python String slice() Examples

In this section, let’s use the different combinations of three paraments to slice the string in Python.

2.3.1 slice() with stop parameter

In the following example, we slice the string to get the first 10 characters from the string. Since we have not specified the start position, by default it considers from the beginning of the string.


# Consider the string
st1='Hello welcome to sparkby examples'
print("String: ",st1)

# Return first 10 characters
print(st1[slice(10)])

# Skip last 10 characters
print(st1[slice(-10)])

# Output:
# String:  Hello welcome to sparkby examples
# Hello welc
# Hello welcome to sparkb

Here, we have only specified the stop parameter as 10. So 10 characters were returned from the start of the string. (Along with space)

2.3.2 slice() with start & stop parameter


# Consider the string
st1='Hello welcome to sparkby examples'

# Slice from 6th position to 13th position
print(st1[slice(6,13)])

# Slice from last 8th position to 15th position
print(st1[slice(-15,-7)])

# Output:
# welcome
# parkby e

Here, we are specifying the start and stop parameters.

  1. We specified the start as 6 and the stop as 13. Slicing will happen from the start of the string. So the returned substring is from the 6th position to the 13th position.
  2. We specified the start as -15 and the stop as -7. Slicing will happen from the last part of the string. So the returned substring is from the last 8th position to the 15th position.

2.3.4 slice() with all three parameters

Now, let’s use the three parameters of slice() to get the slice of the string. Note that the last parameter is the step that skips the specified number of characters.


# Consider the string
st1='Hello welcome to sparkby examples'

# Slice starting to 5th character by skipping 3 characters
print(st1[slice(0,5,3)])

# Slice 5th to 11th character by skipping 2 characters
print(st1[slice(4,11,2)])

# Output:
# Hl
# owlo

Here, we are specifying start, stop and step parameters.

  1. We returned the substring which is sliced from the first character to 5th character by skipping 3 characters.
  2. We returned the substring which is sliced from the 5th character to the 11th character by skipping 2 characters

3. String Array Slicing

You can also extract a sub-string (also called a “slice”) from a python string using the slice syntax [start:stop:step]. Here, the start index is the starting position of the slice, and the stop index is the ending position (the slice includes all characters up to, but not including, the ending position).

You can also leave out the starting or ending index, in which case the slice will start at the beginning of the string or end at the end of the string, respectively. You can also specify a step value in the slicing syntax, which allows you to skip characters in the slice.

3.1 Syntax

Following is the syntax.


# Syntax of string[]
string[start:stop:step]

3.2 Parameters of slice()

  1. start is the first parameter which refers to the starting index position. If it is specified, then the slicing will happen and the substring will be returned from this position.
  2. stop is the second parameter. the slicing will end and the substring will be returned to this position.
  3. step is an optional parameter that skips the number of characters within the slicing.

3.3 Python Array Slicing of String Examples

In this section, let’s use the different combinations of three paraments of slice and see how they behave.

3.3.1 Return the entire string without slicing

This example doesn’t specify any indexes hence, it returns the entire string as-is without slicing.


# Consider the string
st1='Hello welcome to sparkby examples'

# Return the entire string
print(st1[:])

# Output:
# Hello welcome to sparkby examples

3.3.2 – Slice by start and stop parameters

Here, I will use start and stop parameters to get the middle slice of the string. The start index is the starting position of the slice, and the stop index is the ending position (the slice includes all characters up to, but not including, the ending position).


# Consider the string
st1='Hello welcome to sparkby examples'

# Slice starts from 2nd character to 10th character
print(st1[1:10])

# Slice starts from 7th character to 20th character
print(st1[6:20])

# Output:
# ello welc
# welcome to spa

3.3.3 Slice the string by specifying all parameters

Finally, let’s slice the string in python by specifying all parameters start, stop and step. Note that step value in the slicing allows you to skip characters in the slice.


# Consider the string
st1='Hello welcome to sparkby examples'

# Slice from 2nd character to 10th character with step as 3
print(st1[1:10:3])

# Slice from 7th character to 20th character with step as 4
print(st1[6:20:4])

# Output:
# eoe
# wotp

3.3.4 – Slice the string by specifying only start parameter

Here, we specified only the start parameter. In this case, the substring will be returned from the specified start character to end of the string. In the below example,

  1. We are returning the string that starts from 2nd character to end of the string.
  2. We are returning the string that starts from 7th character to end of the string.

# Consider the string
st1='Hello welcome to sparkby examples'

# Slice from 2nd character to end of the string.
print(st1[1:])

# Slice from 7th character  to end of the string.
print(st1[6:])

# Output:
# ello welcome to sparkby examples
# welcome to sparkby examples

3.3.5 – Return the string by specifying only stop parameter

Here, we specified only the stop parameter. In this case, the substring will be returned from the first character to the specified character in the string. In the below example,

  1. We are returning the string that starts from first character to 10th character.
  2. We are returning the string that starts from first character to 20th character.

# Consider the string
st1='Hello welcome to sparkby examples'

# Slice from first to 10th character
print(st1[:10])

# Slice from the first character to 20th character
print(st1[:20])

# Output:
# Hello welc
# Hello welcome to spa

4. Conclusion

In this article, you have learned how to slice the string in Python by using the slice() and array slicing approach. Both these approaches take the start, stop and step indexes. The start index is the starting position of the slice, and the stop index is the ending position (the slice includes all characters up to, but not including, the ending position). You can also leave out the starting or ending index, in which case the slice will start at the beginning of the string or end at the end of the string, respectively. You can also specify a step value in the slicing syntax, which allows you to skip characters in the slice.

Leave a Reply