0% found this document useful (0 votes)
8 views5 pages

Understanding Python Slicing Syntax

The document explains Python slicing, a technique used to extract portions of sequences like strings, lists, and tuples using a specific syntax. It details the syntax, parameters, and provides basic and advanced examples, along with key rules and a summary table. Additionally, it includes multiple-choice questions to test understanding of the slicing concepts.

Uploaded by

julierajen1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Understanding Python Slicing Syntax

The document explains Python slicing, a technique used to extract portions of sequences like strings, lists, and tuples using a specific syntax. It details the syntax, parameters, and provides basic and advanced examples, along with key rules and a summary table. Additionally, it includes multiple-choice questions to test understanding of the slicing concepts.

Uploaded by

julierajen1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🐍 Python Slicing

🔹 What is Slicing?

Slicing is a powerful feature in Python used to extract a portion (subsequence) of a string, list,
or tuple using a specific syntax, Or in other words we can say Python slicing is a powerful
technique used to extract portions of sequences like strings, lists, and tuples. It involves using square
brackets [] with a colon : to specify the start, end, and step of the slice.

🔹 Syntax of Slicing
sequence[start : stop : step]
Parameter Description
start The starting index (inclusive). Defaults to 0 if omitted.
stop The ending index (exclusive). Defaults to end of the sequence if omitted.
step The step or jump between indices. Defaults to 1. Can be negative for reverse.

🔹 Basic Examples
text = "Python"
print(text[1:4]) # "yth" → From index 1 to 3
print(text[:3]) # "Pyt" → From beginning to index 2
print(text[2:]) # "thon" → From index 2 to end
print(text[::2]) # "Pto" → Every 2nd character
print(text[::-1]) # "nohtyP" → Reversed string

🔹 Advanced Understanding with text = "HelloWorld"

String: "HelloWorld"
Indexes:

H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
text[-3:] # 'rld' → From index -3 (i.e., 7) to end
text[7:11] # 'rld' → From index 7 to 10 (index 11 is exclusive)
text[7:] # 'rld' → From index 7 to end

✅ All of the above three give the same result.


⚠️ Important Note
text[-3:0]

 Start = index -3 → 'r'


 Stop = index 0 → 'H'
 ❌ Output = '' (empty)

Because Python slicing moves left to right by default, and here start > stop, it returns
nothing.

✅ Valid Alternative Stop Values for text[-3:]

If you want to extract the last 3 characters ('rld'):

text[-3:] # ✅ Correct
text[7:10] # ✅ Correct
text[7:100] # ✅ Safe — Python doesn’t crash even if stop index exceeds

🔹 Key Rules

 Start is inclusive, stop is exclusive


 Default direction: Left to Right (positive step)
 Negative indices: Count from the end of sequence
 Negative step: Goes from right to left
 Slicing creates a new copy, not a reference

🔹 List and Tuple Slicing


numbers = [1, 2, 3, 4, 5]
print(numbers[::2]) # [1, 3, 5]

data = (10, 20, 30, 40, 50)


print(data[1:4]) # (20, 30, 40)
📌 Summary Table

Slice Meaning Output (text = "HelloWorld")


text[-3:] Last 3 characters 'rld'
text[7:11] From index 7 to 10 'rld'
text[7:] From index 7 to end 'rld'
text[-3:0] Invalid (start > stop in left-to-right) '' (empty string)

📝 Python Slicing – MCQs


1. What will be the output of the following code?
text = "Python"
print(text[1:4])

a) Pyt
b) ytho
c) yth ✅
d) tho

2. Which of the following gives the last 3 characters of the string "HelloWorld"?

a) text[7:] ✅
b) text[6:9]
c) text[8:]
d) text[:3]

3. What will text[::-1] return if text = "Python"?

a) nohtyP ✅
b) Pto
c) yth
d) Error

4. Which of the following slices an entire string?


a) text[0:len(text)-1]
b) text[:len(text)] ✅
c) text[1:]
d) text[:-1]

5. What will be the output of text[-3:0] if text = "HelloWorld"?

a) rld
b) World
c) '' ✅ (empty string)
d) Error

6. What does text[::2] do?

a) Skips every 2 characters


b) Selects every second character ✅
c) Reverses the string
d) Removes vowels

7. In the slice sequence[start:stop], the slicing includes:

a) Only the stop index


b) Only the start index
c) Both start and stop indices
d) Start index (inclusive), stop index (exclusive) ✅

8. What will be the result of this?


text = "DataScience"
print(text[4:10])

a) Science
b) aScienc
c) Scienc ✅
d) Scien
9. What is the result of text[-1] when text = "Code"?

a) C
b) o
c) e ✅
d) d

10. What happens if the stop value in slicing is beyond the string length?
text = "Python"
print(text[4:100])

a) Error
b) Nothing
c) It returns characters from index 4 to end ✅
d) It loops back to the beginning

You might also like