Languages
[Edit]
EN

Python - string slicing

0 points
Created by:
Dirask Community
6300

In this article, we would like to show you how to use string slicing in Python.

Syntax

By using the following slice syntax you can return a range of characters:

string[start_index: end_index]

or

string[start_index: end_index: step]

Where:

  • start_index -¬†¬†index position, from where it will start fetching the characters (default value is 0),
  • end_index -¬†¬†index position, where it will end fetching the characters (default value is¬†end of the string),
  • step - interval between each character (default value is 1)

Note:

string[start_index: end_index: step]

is equivalent to:

string[slice(start_index, end_index, step)]

Positive indexing

In this example, we specify the start index and the end index to return a part of the string using positive indexing only.

text = "dirask is cool"

substring1 = text[0:6]      # dirask    (start from index 0 to 7)
substring2 = text[7:9]      # is
substring3 = text[10:14]    # cool

print(substring1)  # dirask
print(substring2)  # is
print(substring3)  # cool

Output:

dirask
is
cool

Negative indexing

In this example, we specify the start index and the end index to return a part of the string using negative indexing only.

text = "dirask is cool"

substring1 = text[-14:-8]   # dirask    (counting from the end of the string)
substring2 = text[-7:-5]    # is
substring3 = text[-4:14]    # cool

print(substring1)  # dirask
print(substring2)  # is
print(substring3)  # cool

Output:

dirask
is
cool

Slice from the start

In this example, we don't specify the start index to start slicing from the index 0 by default.

text = "dirask is cool"

substring = text[:6]       # dirask    (starts from index 0 by default)

print(substring)  # dirask

Output:

dirask

Slice to the end

In this example, we don't specify the end index to end slicing at the end of the string by default.

text = "dirask is cool"

substring1 = text[10:]      # cool      (from index 10 to the end of the string)
substring2 = text[-4:]      # cool      (from index -4 to the end of the string)

print(substring1)  # cool
print(substring2)  # cool

Output:

cool
cool

Using different steps

1. Positive indexing

In this example, we specify the step to iterate e.g every second value from the start of the string.

text = "ABCD"

substring1 = text[::1]  # print every letter
substring2 = text[::2]  # print every second letter

print(substring1)  # ABCD
print(substring2)  # AC

Output:

ABCD
AC

2. Negative indexing

In this example, we specify the step to iterate e.g every second value from the end of the string.

text = "ABCD"

substring1 = text[::-1]  # print every letter starting from the end
substring2 = text[::-2]  # print every second letter starting from the end

print(substring1)  # DCBA
print(substring2)  # DB

Output:

DCBA
DB

Python - string (popular problems)

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join