0% found this document useful (0 votes)
3 views

Python_Slicing_Tutorial

The document explains Python slicing, a technique for extracting specific parts of sequences like strings, lists, or tuples using the syntax sequence[start:stop:step]. It demonstrates how to reverse a string and lists using slicing with examples, including extracting parts of strings and skipping characters. Additionally, it covers reversing a string using a stop index and provides a breakdown of reversing a substring.

Uploaded by

shivamsv167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_Slicing_Tutorial

The document explains Python slicing, a technique for extracting specific parts of sequences like strings, lists, or tuples using the syntax sequence[start:stop:step]. It demonstrates how to reverse a string and lists using slicing with examples, including extracting parts of strings and skipping characters. Additionally, it covers reversing a string using a stop index and provides a breakdown of reversing a substring.

Uploaded by

shivamsv167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Slicing & Reversing a String

1. What is Slicing in Python?

Slicing is a technique in Python that allows us to extract a specific part of a sequence (like a string,
list, or tuple). It follows this syntax:

sequence[start:stop:step]

start -> The index where slicing begins (inclusive).


stop -> The index where slicing ends (exclusive).
step -> The interval between elements (optional).

2. Reversing a String Using Slicing

We can reverse a string in Python using slicing by setting the step value as -1.

word = "Python"

reversed_word = word[::-1] # Slicing with step -1

print(reversed_word)

Output:
nohtyP

3. More Slicing Examples

Example 2: Extracting Part of a String

text = "Hello, World!"

print(text[0:5]) # Extracts "Hello"

Output:
Hello

Example 3: Skipping Characters with Step


Python Slicing & Reversing a String

word = "ABCDEFGHIJ"

print(word[0:10:2]) # Extract every 2nd character

Output:
ACEGI

Example 4: Reversing Only a Part of a String

text = "PythonProgramming"

print(text[6:18][::-1]) # Reverse "Programming"

Output:
gnimmargorP

4. Reversing Lists Using Slicing

numbers = [1, 2, 3, 4, 5]

print(numbers[::-1]) # Reverse the list

Output:
[5, 4, 3, 2, 1]

5. Common Uses of Slicing

* Extracting parts of strings or lists


* Skipping elements with step
* Reversing sequences
* Working with substrings efficiently

6. Reversing a String Using Stop in Slicing

In Python, when reversing a string using slicing, we usually set the step as -1 ([::-1]). However, we
can also control the reversal by using the stop index.

text = "Python"
Python Slicing & Reversing a String

reversed_part = text[:3:-1]

print(reversed_part)

Output:
noh

7. Breaking Down Example: Reversing Only a Part of a String

Step 1: Extracting 'Programming' Using text[6:18]

text = "PythonProgramming"

extracted_part = text[6:18] # Extracts "Programming"

print(extracted_part)

Output:
Programming

Step 2: Reverse the Extracted Part

reversed_part = extracted_part[::-1] # Reverse "Programming"

print(reversed_part)

Output:
gnimmargorP

You might also like