Python Slicing Step by Step Beginner Guide

Python-Slicing-Step-by-Step-Beginner-Guide
Credit: codingstreets

Overview: Python Slicing

Let’s get started with Python Slicing. Here we will highlight various Python slicing ways using index numbers. From positive slicing to negative slicing with step slicing, we will cover each slicing in detail.

What’s Next? – Python Slicing

In this article, we will explore how to use Python Slicing using various indexing methods. From basic slice notation to various practical examples, moving ahead will cover Python positive slicing, negative slicing, step slicing, empty slices, edge cases and special scenarios, etc.

Table of Contents

What is Python Slicing?

Python slicing is the process of extracting the sequence of values from the starting position to ending position. Python allows exacting values from sequences like strings, lists, and tuples.

What is Python Slicing Syntax?

				
					Syntax: Sequence[Start:Stop:Step]
				
			

What are Python Slicing related terms?

Sequence: Like – list, tuple, strings

Start: starting index (inclusive, default is 0)

Stop: ending index (exclusive, default is end of string)

Step: how many characters to move each time (default is 1). Negative step (like: -1, -2, -3,…) means reverse direction.

Key Points to Remember

1. Indices start at 0

2. Stop index is exclusive (not included in result)

3. Negative indices count from the end. Negative index number starts from -1.

4. Omitted values use defaults: start=0, stop=len(seq), step=1

5. Slicing never raises IndexError for out-of-range indices

6. Slice assignment can change list size

Quick Reference Table

python-slicing
credit: codingstreets

Example:

txt =  “PYTHON”

Positive Indices: 0=P, 1=Y, 2=T, 3=H, 4=O, 5=N 

(index number at which position the characters are stored)

txt =  “PYTHON”

Negative Indices:  -6 = P, -5 = Y, -4 = T, -3 = H, -2 = O, -1 = N 

Positive Indexing

#entire string

				
					print(txt[:]) 
#Output: PYTHON
				
			

Explanation: First colon is used for the Start parameter. Omit parameter for Stop. By default it copies the entire string.

#from index 3 to end

				
					print(txt[3:]) 
#Output: HON
				
			

Explanation: Start = 3 and omit parameter for Stop. By default Stop takes elements from beginning to stop-1.

In this case, the Stop parameter is not defined.

Stop = len(variable) → length of the string

len(txt) = 6   #get the length of the string.

Stop-1 = 6-1 = 5. 5 is the last index number till Stop parameter include characters from txt.

#from start to index 4

				
					print(txt[:5]) 
#Output: PYTHO
				
			

Explanation: Start parameter is not defined, i.e., by default takes character from starting. Stop = 5, i.e., takes characters till index 4 because → Stop-1 = 5-1 = 4.

#from index 2 to 5

				
					print(txt[2:6]) 
#Output: THON
				
			

Explanation: Start = 2 and Stop = 6, since stop-1; therefore, output is THON.

Negative Indexing

#from last third index to end → (last 3 characters)

				
					print(txt[-3:]) 
#Output: HON
				
			

Explanation: Start = -3. Since, the number is negative; therefore, the index starts from reverse order. Stop is not defined; therefore, by default includes till end.

#from start index till index 2

				
					print(txt[:-3]) 
#Output: PYT
				
			

Explanation: Start index not defined, i.e, by default slicing starts from 0 index. Stop = -3, but Stop excludes the last index; therefore, slicing done from index 0 to 2.

#from index 1 to 3

				
					print(txt[-5:-2]) 
#Output: YTH
				
			

Explanation: Start = -5, and Stop = -2, meaning negative indexing. Slicing is done in reverse order.

				
					print(txt[-2:-2]) 
#Output: no output
				
			

Explanation: Start = -2, and Stop = -2 meaning negative indexing. Since, start and stop have the same index number; therefore, nothing prints out because Stop excludes the last index.

Step Indexing

#from entire string get every 2nd character

				
					print(txt[::2]) 
#Output: PTO
				
			

Explanation: Since start and stop position is not defined so default slicing selects the entire string. Step = 2, meaning get every 2nd character from the string.

#from entire string get every 2nd character in reverse order

				
					print(txt[::-2])
#Output: NHY
				
			

Explanation: Since start and stop position is not defined so default slicing selects the entire string. Step = -2, meaning slicing is considered in reverse order.

#from index number 1 to 4 get every 2nd character

				
					print(txt[1:5:2])
#Output: YH
				
			

Explanation: Start = 1 and Stop = 5, select the entire string between the 1 to 5. Step = 2, meaning get every 2nd character from the string.

#from index number 4 to 1 get every 3rd character in reverse order

				
					print(txt[4:1:-3])
#Output: O
				
			

Explanation: Start = 4 and Stop = 1. Since Stop = -3; therefore negative indexing is applied. At index 4 character ‘O’ is stored and Stop does not include the last index; therefore, not any other character left in range to slice.

Now it’s Your Turn!

				
					Channel = "CODINGSTREETS"
				
			

Perform the Python slice on the above String. Write the index number to slice the string and match your outputs with below.

  1. CODINGSTREETS → Positive slicing
  2. STEERTSGNIDOC  → Reserve slicing
  3. SESIC → get every 3rd character from string, in reverse order
  4. INGSTREE → get characters from index 3 to 11
  5. STREE → get substring with negative slicing
  6. DNSRE → get every 2nd character from index 2 to 10
  7. TETGI → get every 2nd character from index 11 to 2, in reverse order

Conclusion: Python Slicing

Python slicing is a powerful feature that enables efficient sequence manipulation through its clean [start:stop:step] syntax. By mastering both positive and negative indexing along with step values, you can effortlessly extract, reverse, or skip elements in strings, lists, and other sequences. Ultimately, slicing not only enhances code readability and conciseness but also exemplifies Python’s philosophy of making common operations simple and expressive.

Recent Articles