Important Notice:

Indexing in Python Strings

Indexing in Python Strings

6 views 2 min read

Indexing in Python Strings :-

Indexing (इंडेक्सिंग) वह प्रक्रिया है जिसके द्वारा String (स्ट्रिंग) के प्रत्येक Character (अक्षर) को उसकी Position (स्थिति) के आधार पर Access (प्राप्त) किया जाता है।

Python में String एक Sequence (क्रम) होती है, इसलिए उसके प्रत्येक Character का एक Index Number (इंडेक्स संख्या) होता है। Index की सहायता से हम किसी भी Character को आसानी से प्राप्त कर सकते हैं।

Python में Indexing 0 (शून्य) से शुरू होती है। अर्थात् पहला Character हमेशा Index 0 पर होता है।

English

Indexing is the process of accessing individual characters of a string using their position (index).

Since a string is a sequence of characters, every character has a unique index number. Using these index numbers, we can access any character in the string.

In Python, indexing starts from 0, so the first character is always at index 0.

Syntax (सिंटैक्स)-

string_name[index]

Example -

language = "Python"

print(language[0])
print(language[3])
print(language[5])
 
output-
P
h
n
Types of Indexing (इंडेक्सिंग के प्रकार) :-

Python में दो प्रकार की Indexing होती है:

  1. Positive Indexing (धनात्मक इंडेक्सिंग)
  2. Negative Indexing (ऋणात्मक इंडेक्सिंग)
Positive Indexing (धनात्मक इंडेक्सिंग) :-

Positive Indexing (धनात्मक इंडेक्सिंग) वह विधि (Method) है जिसमें String (स्ट्रिंग) के Characters (अक्षरों) को बाएँ (Left) से दाएँ (Right) की ओर Index Number (इंडेक्स संख्या) प्रदान की जाती है।

Python में Positive Indexing हमेशा 0 (शून्य) से शुरू होती है। अर्थात् String का पहला Character Index 0 पर, दूसरा Character Index 1 पर, तीसरा Character Index 2 पर होता है।

Positive Indexing का उपयोग किसी String के किसी विशेष Character को उसकी Position (स्थिति) के आधार पर Access (प्राप्त) करने के लिए किया जाता है।

English

Positive Indexing is a method of assigning index numbers to the characters of a string from left to right.

In Python, positive indexing always starts from 0. This means the first character is at index 0, the second character is at index 1, and so on.

Positive indexing is used to access a specific character in a string based on its position.

Example -

text = "Python"

print(text[0])
print(text[1])
print(text[2])
print(text[3])
print(text[4])
print(text[5])
 
Output-
P
y
t
h
o
n
 
Example 2:
text = "Programming"
i=5
print(text[i])
 
Output-
a
 
note:-

यदि दिया गया Positive Index String की Length से बड़ा हो, तो IndexError आता है।

text = "Python"

print(text[10])
 
Output:-
IndexError: string index out of range
note-

हम len() Function की सहायता से अंतिम Character प्राप्त कर सकते हैं।

name = "Python"
print(name[len(name)-1])
Output-
n
Negative Indexing (ऋणात्मक इंडेक्सिंग) :-

Negative Indexing (ऋणात्मक इंडेक्सिंग) वह विधि (Method) है जिसमें String (स्ट्रिंग) के Characters (अक्षरों) को दाएँ (Right) से बाएँ (Left) की ओर Index Number (इंडेक्स संख्या) प्रदान की जाती है।

Python में अंतिम (Last) Character का Index हमेशा -1 होता है। उससे पहले वाले Character का Index -2, फिर -3, और इसी प्रकार आगे बढ़ता है।

Negative Indexing का उपयोग विशेष रूप से तब किया जाता है जब हमें String के अंत (End) से Characters को Access (प्राप्त) करना हो।

English

Negative Indexing is a method of assigning index numbers to the characters of a string from right to left.

In Python, the last character of a string always has the index -1. The second last character has the index -2, the third last has -3, and so on.

Negative indexing is mainly used when we want to access characters from the end of a string.

Example-

text = "Python"
print(text[-1])
print(text[-2])
print(text[-3])
print(text[-4])
print(text[-5])
print(text[-6])
 
Output-
n
o
h
t
y
P
 

Related Notes