• Home
  • 4.5 String Slicing – Part 2

4.5 String Slicing – Part 2

View Categories

4.5 String Slicing – Part 2

4 min read

In the previous lesson, you learned how to extract a portion of a string using the start and stop indices.

Python slicing also supports a third value called the step. The step determines how many characters Python skips while extracting the string.

Using the step value, you can select alternate characters, skip characters, or even reverse an entire string.

The complete slicing syntax is:

string[start:stop:step]

Syntax #

string_name[start:stop:step]

Components #

Component Description
string_name The string to be sliced.
start Starting index (inclusive).
stop Ending index (exclusive).
step Number of positions to move after selecting each character.

Important: If the step is omitted, Python uses a default value of 1.


Example 1: Using a Step of 2 #

main.py

word = "Programming"

print(word[0:11:2])

Output #

Pormig

Explanation #

  • The slice starts at index 0.
  • The step value is 2.
  • Python selects every second character.
  • Characters at indices 0, 2, 4, 6, 8, and 10 are returned.

Example 2: Omitting the Start Index #

main.py

word = "Programming"

print(word[:8:2])

Output #

Porm

Explanation #

  • The slice begins from index 0.
  • It stops before index 8.
  • Every second character is selected.

Example 3: Omitting the Stop Index #

main.py

word = "Programming"

print(word[3::2])

Output #

gamn

Explanation #

  • The slice starts at index 3.
  • Since no stop index is specified, Python continues until the end.
  • Every second character is selected.

Example 4: Step Value of 3 #

main.py

word = "ABCDEFGHIJKL"

print(word[::3])

Output #

ADGJ

Explanation #

  • The slice starts from the beginning.
  • Python selects every third character.
  • The returned string contains characters at indices 0, 3, 6, and 9.

Example 5: Reversing a String #

main.py

word = "Python"

print(word[::-1])

Output #

nohtyP

Explanation #

  • A negative step moves through the string backward.
  • [::-1] starts from the end and moves one character at a time toward the beginning.
  • This is the most common way to reverse a string.

Example 6: Reverse Partial Slice #

main.py

word = "Programming"

print(word[8:3:-1])

Output #

mmarg

Explanation #

  • The slice starts at index 8.
  • The stop index is 3, which is excluded.
  • Since the step is -1, Python moves backward one character at a time.

Example 7: Last Three Characters #

main.py

word = "Computer"

print(word[-3:])

Output #

ter

Explanation #

  • Negative indexing can also be used with slicing.
  • -3 refers to the third character from the end.
  • The slice continues until the end of the string.

Example 8: Every Second Character from the End #

main.py

word = "Python"

print(word[::-2])

Output #

nhy

Explanation #

  • The slice begins from the last character.
  • Python moves backward two positions at a time.
  • Every second character from the end is selected.

Example 9: User Input #

main.py

text = input("Enter a word: ")

print(text[::2])

Sample Input #

Elephant

Output #

Eehn

Explanation #

  • The user enters a string.
  • The slice selects every second character.
  • The original string remains unchanged.

Example 10: Copying Using Step #

main.py

word = "Python"

copy = word[::]

print(copy)

Output #

Python

Explanation #

  • Omitting all three values creates a complete copy of the string.
  • The default step is 1.
  • The copied string is identical to the original.

Common Mistakes #

1. Using a Step Value of Zero #

Incorrect

word = "Python"

print(word[::0])

Output #

ValueError: slice step cannot be zero

Reason

The step value cannot be zero because Python would not know how to move through the string.


2. Forgetting That a Negative Step Reverses Direction #

Incorrect

word = "Python"

print(word[1:5:-1])

Output #

Reason

When using a negative step, the start index should usually be greater than the stop index.

Correct

print(word[5:1:-1])

3. Assuming Slicing Changes the Original String #

Incorrect

word = "Python"

word[::-1]

print(word)

Output #

Python

Reason

Slicing always returns a new string.
The original string remains unchanged.

Correct

word = word[::-1]

print(word)

4. Expecting the Stop Index to Be Included #

Incorrect

word = "Python"

print(word[0:5:2])

Incorrect Expectation #

Pton

Actual Output #

Pto

Reason

The stop index is always excluded, regardless of the step value.


Best Practices #

  • Use the step value to extract patterns such as alternate characters.
  • Use [::-1] to reverse a string quickly and efficiently.
  • Remember that the stop index is always excluded.
  • Use negative indices with slicing when working from the end of a string.
  • Store the sliced result if it will be used later.

Key Points to Remember #

  • The complete slicing syntax is string[start:stop:step].
  • The step determines how many positions Python moves after selecting each character.
  • The default step value is 1.
  • A negative step traverses the string backward.
  • [::-1] is the standard way to reverse a string.
  • The stop index is always excluded from the slice.
  • String slicing always returns a new string and does not modify the original string.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *