Slicing

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Introduction to String Slicing

String slicing is a way to extract a part of a string (a substring). It's like cutting a piece from a string.

The basic syntax for slicing is: string[start:end] Where: - start is the index where the slice starts (inclusive) - end is the index where the slice ends (exclusive)

codemon_name = "Aquachu" # Get the first 4 characters first_part = codemon_name[0:4] print(first_part) # Get the last 3 characters last_part = codemon_name[4:7] print(last_part)

Remember that in Python, string indices start at 0, not 1. So the first character is at index 0, the second at index 1, and so on.

Important Note

The end index is NOT included in the slice. So [0:4] gives you characters at positions 0, 1, 2, and 3, but not 4.

Exercise 2 — Basic String Slicing

What will the following code print?

codemon = "Florachu" print(codemon[0:5])
Options
• Flora
• Florac
• Flor
• Florach

Hint : Remember: The slice [0:X] includes characters at indices 0, 1, 2, .. and X - 1. The end index (X) is NOT included.

Exercise 3 — Smarter Slicing

Python provides shortcuts for string slicing: - If you omit the start index, Python uses 0 as the default start - If you omit the end index, Python uses the length of the string as the default end

codemon = "Pyrochu" print(codemon[:4]) # Same as codemon[0:4] print(codemon[4:]) # Same as codemon[4:len(codemon)] print(codemon[:]) # Same as codemon[0:len(codemon)]

Exercise 4 — Slicing Expression Results

Match each slicing expression with its result for the string codemon = "Aquachu"

Items to match
• codemon[:3]
• codemon[3:]
• codemon[2:5]
• codemon[:]
Definitions
• Aqu
• achu
• uac
• Aquachu

Exercise 5 — Negative Indices in Slicing

Python allows negative indices in slicing. Negative indices count from the end of the string: - -1 refers to the last character - -2 refers to the second-to-last character - And so on...

codemon = "Florachu" # Last 3 characters print(codemon[-3:]) # Same as codemon[5:8] # Everything except the last 2 characters print(codemon[:-2]) # Same as codemon[0:6] # From the third character to the second-to-last character print(codemon[2:-2]) # Same as codemon[2:6]

Visualizing Negative Indices

For the string "Florachu": Indices: 0 1 2 3 4 5 6 7 Characters: F l o r a c h u Neg indices:-8 -7 -6 -5 -4 -3 -2 -1

Exercise 6 — Practice with Negative Slicing

Complete the following code to get the specified parts of the Codémon name using negative indices.

Hint : Remember: To get the last N characters, use [-N:]. To exclude the last N characters, use [:-N].

Exercise 7 — Slicing with Step

Slicing can include a third parameter: the step. The full syntax is: string[start:end:step]

The step determines how many characters to move forward. For example, if the step is 2, it means you take every second character.

codemon = "Florachu" # Every character (step=1 is default) print(codemon[::1]) # Same as codemon[:] # Every second character print(codemon[::2]) # F, o, a, h # Every third character print(codemon[::3]) # F, r, h

The step parameter can also be negative, which means traversing the string from right to left (in reverse).

codemon = "Florachu" # Reverse the string print(codemon[::-1]) # uhcarolF # Every second character, in reverse print(codemon[::-2]) # ucrl

Exercise 8 — Slicing with Step Parameter

What will the following code print?

codemon = "Pyrochu" print(codemon[1:6:2])
Options
• yoh
• yroc
• yoc
• yo

Hint : Remember: The slice [a:b:c] starts at index a, stops before index b, and takes every c-th character.

Exercise 9 — Reverse String with Slicing

Complete the code to reverse the Codémon name using string slicing.

Hint : Remember: To reverse a string completely, you can use [::-1].

Exercise 10 — Advanced Slicing Expressions

Match each slicing expression with its result for the string codemon = "Florachu"

Items to match
• codemon[::2]
• codemon[::-1]
• codemon[1:7:3]
• codemon[-1:-7:-2]
Definitions
• Foah
• uhcarolF
• la
• ucr

Exercise 11 — Codémon Secret Code Generator

Create a secret code generator for your Codémon! Choose a Codémon name and write a program that: - Extracts the first 4 characters using slicing and stores it in a variable - Extracts the last 3 characters using negative slicing and stores it in a variable - Reverses the last 3 characters using [::-1] and stores it in a variable - Gets every second character from the full name using step and stores it in a variable - Concatenates all these variables together to create a unique secret code

Starter code
# Create your secret code here
Back to the course