• Home
  • 4.4 String Slicing – Part 1

4.4 String Slicing – Part 1

View Categories

4.4 String Slicing – Part 1

4 min read

String indexing allows you to access a single character. However, many times you need to retrieve multiple consecutive characters from a string.

Python provides string slicing to extract a portion of a string.

A slice creates a new string containing the selected characters from the original string.

In this lesson, we will learn the basic slicing syntax using the start index and stop index. More advanced slicing techniques such as the step value and reversing strings will be covered in the next lesson.


Syntax #

string_name[start:stop]

Components #

Component Description
string_name The string to be sliced.
start Index where the slice begins (inclusive).
stop Index where the slice ends (exclusive).
: Slice operator.

Important: The character at the start index is included, while the character at the stop index is excluded.


Example 1: Extracting the First Three Characters #

main.py

language = "Python"

print(language[0:3])

Output #

Pyt

Explanation #

  • The slice starts at index 0.
  • The slice stops before index 3.
  • Characters at indices 0, 1, and 2 are returned.
  • The character at index 3 is not included.

Example 2: Extracting Characters from the Middle #

main.py

language = "Python"

print(language[2:5])

Output #

tho

Explanation #

  • The slice begins at index 2.
  • It stops before index 5.
  • Characters t, h, and o are returned.

Example 3: Omitting the Start Index #

main.py

language = "Python"

print(language[:4])

Output #

Pyth

Explanation #

  • When the start index is omitted, Python assumes it is 0.
  • The slice begins from the first character.
  • It stops before index 4.

Example 4: Omitting the Stop Index #

main.py

language = "Python"

print(language[3:])

Output #

hon

Explanation #

  • The slice starts at index 3.
  • Since no stop index is specified, Python continues until the end of the string.
  • The returned string is "hon".

Example 5: Copying an Entire String #

main.py

text = "Programming"

copy = text[:]

print(copy)

Output #

Programming

Explanation #

  • Omitting both the start and stop indices selects the entire string.
  • A new string identical to the original is created.

Example 6: User Input #

main.py

name = input("Enter your name: ")

print(name[0:3])

Sample Input #

Michael

Output #

Mic

Explanation #

  • The user enters a string.
  • The slice extracts the first three characters.
  • The original string remains unchanged.

Example 7: Slicing a Numeric String #

main.py

number = "987654321"

print(number[3:6])

Output #

654

Explanation #

  • Although the string contains digits, each digit is treated as a character.
  • The slice returns characters from index 3 up to, but not including, index 6.

Example 8: Extracting a Word #

main.py

sentence = "Hello World"

print(sentence[6:11])

Output #

World

Explanation #

  • Spaces are counted as characters.
  • The letter W begins at index 6.
  • The slice returns "World".

Example 9: Slice Returning an Empty String #

main.py

text = "Python"

print(text[2:2])

Output #

Explanation #

  • The start and stop indices are the same.
  • No characters exist between these positions.
  • Therefore, Python returns an empty string.

Example 10: Checking the Return Type #

main.py

word = "Python"

part = word[1:4]

print(part)
print(type(part))

Output #

yth
<class 'str'>

Explanation #

  • String slicing always returns another string.
  • The extracted substring is stored in part.
  • Its data type is str.

Common Mistakes #

1. Assuming the Stop Index is Included #

Incorrect

word = "Python"

print(word[0:3])

Incorrect Expectation #

Pyth

Actual Output #

Pyt

Reason

The stop index is excluded from the slice.


2. Reversing the Start and Stop Indices #

Incorrect

word = "Python"

print(word[4:2])

Output #

Reason

When the start index is greater than the stop index (without using a negative step), Python returns an empty string.


3. Assuming Slicing Modifies the Original String #

Incorrect

word = "Python"

word[0:3]

print(word)

Output #

Python

Reason

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

Correct

word = word[0:3]

print(word)

4. Using Floating-Point Indices #

Incorrect

word = "Python"

print(word[1.0:4])

Output #

TypeError: slice indices must be integers or None or have an __index__ method

Reason

Slice indices must be integers.


Best Practices #

  • Remember that the start index is included and the stop index is excluded.
  • Use omitted indices ([:stop] and [start:]) to simplify your code.
  • Store the sliced string if you need to use it later.
  • Use descriptive variable names for extracted substrings.
  • Remember that slicing never changes the original string.

Key Points to Remember #

  • String slicing extracts a portion of a string.
  • The basic slicing syntax is string[start:stop].
  • The start index is included.
  • The stop index is excluded.
  • Omitting the start index begins the slice from index 0.
  • Omitting the stop index continues the slice to the end of the string.
  • String slicing always returns a new string.

Powered by BetterDocs

Leave a Reply

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