learnai2k@gmail.com

Section 2 – String Indexing & Slicing

In Python, you can treat a string like a list of characters. 

If you want to extract each letter or parts of string, then there are two operators to achieve this task called as indexing and slicing. 

Every Python programmer is using these operations in real time projects.

a) Accessing characters by Index:

Each character has a position number in a string and it starts from 0 on the left side. One can use these numbers to access a particular character.

Example:



Explanation:

•    text[0]: It gives the first character (P)of string text.
•    text[1]: It  gives the second character (y) of string text.

Real-World Application:

Suppose, you have an email id (as an user input) and you want to check if first character is a letter or not.


 
Why it matters:

There are many ways in which indexing helps you like:

•  It is used in companies to automatically generate initials for employees in the attendance system.
• It is used to check whether a file is an image, document, or Python file in a file automation script.

b) Negative Indexing:

It means counting characters from the end of the string. -1 represents the last character, -2 represents second last and so on.

Example:


 
Real-world Application:

Suppose you have multiple file names, and you want to check if a file name ends with a particular extension or not.

c) Slicing Strings:

Slicing means, you can extract a portion or subset of a string by using a range of indexes.

Syntax: 
string [start: end]
start: It represents an index where slicing begins (inclusive)
end: It represents an index where slicing stops (exclusive)

Example:


 
Explanation:

•    text[0:6] : It starts from index 0 and ends before 6; hence, it returns "Python."
•    text[7:] : It starts from 7 till the end; hence, it returns “Programming”
•    text[:6] : It omits start index (assumes 0) and ends before 6; hence it returns Python

Real World Application :

If you want to extract a username from a given email id, then it is used.

Example:


 
Why it matters:

It is mainly used in three areas (as written below):

•    Data cleaning
•    Preprocessing
•    Text extraction

d) Out of Range:

If you try to extract a string beyond the string’s length, Python returns an empty string and doesn’t throw an error or crash.

Example :


 
Explanation:

"Hi" string has only 2 characters. The requested slice from index position 5 to 10 doesn’t exist, so Python returns only an empty string.

Why it matters: 

Suppose you have an automation script that processes text automatically; handling out-of-range indexes helps keep your program error-free and stable.

e) Length of a string (len() function):-

It helps you find out how many characters are in a string using the len() function.

Example:


 
Explanation:

len(text)function returns 6 because it has six characters.

Real-World Application:

It is used in scenarios such as checking whether the length of a password meets specific criteria or not.

Example : 



Why it matters :

Mainly, the length of a string helps in three ways:
a) Data validation
b) API development
c) AI input preprocessing

g)    Checking for a Substring (‘in’ Keyword):

It helps you to check whether one string exists inside another string using the in keyword.

Example:


 
Explanation:

Since the expression "Python" is present in the str1 string, it returns True.

Real-World Application:

It can be used to search for keywords in user input, emails, or website content.

Example:


 
Why it matters:
It is helpful in scenarios where you want to check if a small piece of text exists within a larger string or not.
For example, on web pages, a concept known as “find matching words” is used by search engines.
Filters (such as email spam filters) use it to detect certain words in a message.