DEV Community

kavin suresh
kavin suresh

Posted on

Indexing and slicing

Hi everybody
I am Kavin
I going to write a blog which I learnt in my python class.

Indexing

Indexing is nothing but a position. Indexing refers to accessing individual elements of a sequence, such as a string.

In Python, strings are sequences of characters, and each character in a string has a position, known as an index.
eg:

word = K A V I N
       ^ ^ ^ ^ ^
index= 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

Basic Indexing
eg:

name="kavin"
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])

'k'
'a'
'v'
'i'
'n'
Enter fullscreen mode Exit fullscreen mode

Negative Indexing
Python also supports negative indexing, which allows you to access characters from the end of the string.
eg:

name="kavin"
print(name[-1])
print(name[-2])
print(name[-3])
print(name[-4])
print(name[-5])

'n'
'i'
'v'
'a'
'k'
Enter fullscreen mode Exit fullscreen mode

Combining Positive and Negative Indexing
You can mix positive and negative indexing to access different parts of a string.
eg:

word='Indexing'
print(word[0]) 
print(word[-1])  
print(word[2])
print(word[-3]) 

 'I'
 'g'
 'd'
 'i'
Enter fullscreen mode Exit fullscreen mode

Real-World Examples

** Initials of a Name**
eg:

full_name = "Parotta Salna"
initials = full_name[0] + full_name[8]
print(initials) 

PS
Enter fullscreen mode Exit fullscreen mode

Accessing File Extensions
eg:

filename = "document.pdf"
extension = filename[-3:]
print(extension) 

'pdf'
Enter fullscreen mode Exit fullscreen mode

Slicing

Slicing enables you to create a new string by extracting a subset of characters from an existing string.

Basic Slicing
The basic syntax for slicing is:

string[start:stop]
Enter fullscreen mode Exit fullscreen mode

Where:

start is the index where the slice begins (inclusive).
stop is the index where the slice ends (exclusive).

Simple Slicing
eg:

text = "Hello, World!"
print(text[0:5]) 

'Hello'
Enter fullscreen mode Exit fullscreen mode

Omitting Indices

You can omit the start or stop index to slice from the beginning or to the end of the string.
eg:

text = "Python Programming"
print(text[:6])  

'Python'
Enter fullscreen mode Exit fullscreen mode

Slicing with Step

You can include a step to specify the interval between characters in the slice. The syntax is:

string[start:stop:step]
Enter fullscreen mode Exit fullscreen mode

eg:

text = "abcdefghij"
print(text[0:10:2])  

'acegi'
Enter fullscreen mode Exit fullscreen mode

Negative Indices and Step

Negative indices count from the end of the string, and a negative step allows you to slice in reverse order.
eg with Negative Indices:

text = "Python"
print(text[-3:])  

'hon'
Enter fullscreen mode Exit fullscreen mode

eg with reversing a string :

text = "Reverse"
print(text[::-1]) 

'esreveR'
Enter fullscreen mode Exit fullscreen mode

Real-World Examples

1.Extracting File Extensions

filename = "report.pdf"
extension = filename[-3:]
print(extension)  

'pdf'
Enter fullscreen mode Exit fullscreen mode

2.Getting a Substring

quote = "To be or not to be, that is the question."
substring = quote[9:17]
print(substring)  

'not to be'
Enter fullscreen mode Exit fullscreen mode

3.Parsing Dates

date = "20230722"
year = date[:4]
month = date[4:6]
day = date[6:]
print(f"Year: {year}, Month: {month}, Day: {day}")

Year: 2023, Month: 07, Day: 22
Enter fullscreen mode Exit fullscreen mode

Advanced Slicing Techniques

1.Skipping Characters

text = "abcdef"
print(text[::2]) 

'ace'
Enter fullscreen mode Exit fullscreen mode

2.Slicing with Negative Step

text = "abcdefghij"
print(text[::-2])  

'jhfdb'
Enter fullscreen mode Exit fullscreen mode

This the things which I learnt I my class.
Thank you

Top comments (0)