PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python String Indexing (Positive & Negative) and Slicing [start:stop:step] (स्ट्रिंग इंडेक्सिंग (ऋणात्मक इंडेक्स) और स्लाइसिंग [start:stop:step]) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python string indexing accesses single characters via 0-based positive indices (0 to n-1) or negative indices (-1 to -n), raising IndexError when out of bounds. Slicing s[start:stop:step] extracts half-open substrings [start, stop) that clamp out-of-range bounds safely without errors and reverse strings cleanly via s[::-1].
अध्याय सार: पायथन स्ट्रिंग इंडेक्सिंग 0-आधारित धनात्मक इंडेक्स (0 से n-1) या ऋणात्मक इंडेक्स (-1 से -n) द्वारा किसी एक अक्षर को प्राप्त करती है और सीमा पार होने पर IndexError देती है। स्लाइसिंग s[start:stop:step] अर्ध-खुले अंतराल [start, stop) पर सबस्ट्रिंग निकालती है, सीमा से बाहर जाने पर एरर नहीं देती और s[::-1] से स्ट्रिंग को उल्टा करती है।
Python String Indexing (Positive & Negative) and Slicing [start:stop:step]: Detailed Notes & Architecture (स्ट्रिंग इंडेक्सिंग (ऋणात्मक इंडेक्स) और स्लाइसिंग [start:stop:step])
1. Positive (0-Based) and Negative Indexing (धनात्मक (0-आधारित) और ऋणात्मक इंडेक्सिंग)
[EN] In a string s of length n = len(s), positive indexing starts at 0 for the first character and ends at n - 1 for the last character, moving left to right. Negative indexing provides convenient right-to-left access: s[-1] is the last character, s[-2] the second-to-last, and s[-n] the first character (internally mapped to n + idx). Attempting to index outside [-n, n - 1] (including any index on an empty string '') raises IndexError: string index out of range; floating-point indices like s[1.0] raise TypeError.
[HI] लंबाई n की स्ट्रिंग में धनात्मक इंडेक्स बाएँ से दाएँ 0 (पहला अक्षर) से n - 1 (अंतिम अक्षर) तक चलता है। ऋणात्मक इंडेक्स दाएँ से बाएँ आसान पहुँच देता है: s[-1] अंतिम अक्षर और s[-n] पहला अक्षर दर्शाता है (आंतरिक रूप से n + idx)। [-n, n - 1] सीमा के बाहर इंडेक्स देने पर IndexError आता है और फ्लोट इंडेक्स (जैसे s[1.0]) देने पर TypeError आता है।
2. Slicing Syntax [start:stop:step] and Clamping Rules (स्लाइसिंग सिंटैक्स [start:stop:step] और बाउंड्री क्लैम्पिंग नियम)
[EN] The slice expression s[start:stop:step] extracts characters from start (inclusive) up to, but excluding, stop (half-open interval [start, stop)), advancing by step (default 1). When step > 0, omitting start defaults to 0 and omitting stop defaults to len(s); thus s[:k] yields the first k characters and s[k:] yields everything from index k onward. Unlike single-character indexing, slicing never raises IndexError for out-of-range start or stop values—Python silently clamps bounds to valid endpoints (e.g., 'PY'[0:50] -> 'PY' and 'PY'[10:20] -> '').
[HI] स्लाइस s[start:stop:step] start (शामिल) से शुरू होकर stop से ठीक पहले (stop अपवर्जित) तक प्रत्येक step (डिफ़ॉल्ट 1) के अंतराल पर अक्षर निकालता है। step > 0 होने पर start छोड़ने से 0 और stop छोड़ने से len(s) माना जाता है; अतः s[:k] पहले k अक्षर और s[k:] इंडेक्स k से अंत तक के अक्षर देता है। इंडेक्सिंग के विपरीत, स्लाइसिंग में सीमा से बाहर के start या stop देने पर कभी IndexError नहीं आता, बल्कि पायथन उन्हें स्वतः वैध सीमा तक सीमित (clamp) कर देता है।
3. Negative Step, Reversal (s[::-1]), and Direction Mismatch (ऋणात्मक स्टेप, रिवर्सल (s[::-1]) और दिशा विसंगति)
[EN] When step is negative, slicing traverses right-to-left: omitted start defaults to -1 (last character) and omitted stop runs past index 0 to the start of the string, making s[::-1] the idiomatic way to reverse a string. If start and stop contradict the step direction (such as s[5:2] with step +1, or s[1:4:-1] with step -1), Python returns an empty string ''. Specifying step = 0 (e.g., s[::0]) raises ValueError: slice step cannot be zero.
[HI] जब step ऋणात्मक होता है तो स्लाइसिंग दाएँ से बाएँ चलती है: ऐसे में डिफ़ॉल्ट start -1 (अंतिम अक्षर) और डिफ़ॉल्ट stop शुरुआत से भी आगे तक होता है, जिससे s[::-1] पूरी स्ट्रिंग को उल्टा कर देता है। यदि start और stop की दिशा step के विपरीत हो (जैसे s[5:2] या s[1:4:-1]), तो परिणाम खाली स्ट्रिंग '' आता है। step का मान 0 (s[::0]) देने पर ValueError: slice step cannot be zero उत्पन्न होता है।
4. The slice() Object and Key Slicing Identities (slice() ऑब्जेक्ट और स्लाइसिंग के प्रमुख गुणधर्म)
[EN] Internally, Python translates s[a:b:c] into s.__getitem__(slice(a, b, c)); named slice objects (e.g., first_three = slice(0, 3)) can be stored in variables and reused across sequences. A fundamental identity holds for any integer i: s[:i] + s[i:] == s, making rotation or splitting lossless. Furthermore, mixing positive and negative indices in one slice (e.g., s[1:-1] to strip the first and last character) is completely valid and widely used.
[HI] आंतरिक रूप से पायथन s[a:b:c] को s.__getitem__(slice(a, b, c)) में बदलता है; आप slice(a, b, c) ऑब्जेक्ट बनाकर कई सीक्वेंस पर पुनः उपयोग भी कर सकते हैं। किसी भी पूर्णांक i के लिए s[:i] + s[i:] == s हमेशा सत्य रहता है। एक ही स्लाइस में धनात्मक और ऋणात्मक इंडेक्स को मिलाना (जैसे पहले और अंतिम अक्षर को हटाने हेतु s[1:-1]) पूरी तरह वैध है।
Syntax, Formula & Technical Example (Python String Indexing (Positive & Negative) and Slicing [start:stop:step])
s = 'COMPUTERS'
print(s[0], s[-1], s[-9]) # C S C
print(s[0:4], s[3:]) # COMP PUTERS
print(s[::2]) # CMUES
print(s[::-1]) # SRETUPMOC
print(repr(s[5:2]), s[5:2:-1]) # '' TUP
print(s[2:50]) # MPUTERS
Python String Indexing (Positive & Negative) and Slicing [start:stop:step] Quick Reference & Comparison Table
| Index / Slice Form | Extracted Result on 'PYTHON' | Key Exam Fact |
|---|---|---|
| s[-1] / s[-6] | 'N' / 'P' | Negative index maps to len(s) + idx |
| s[1:4] | 'YTH' | Start inclusive, stop index 4 excluded |
| s[::-1] | 'NOHTYP' | Negative step reverses entire string |
| s[2:99] | 'THON' | Out-of-range slice clamps; no IndexError |
Python String Indexing (Positive & Negative) and Slicing [start:stop:step] Top 20 Bilingual MCQs with Answers
Q1. For s = 'COMPUTER', what is the output of s[-3]?
(s = 'COMPUTER' के लिए s[-3] का मान क्या होगा?)
(A) 'U'
(B) 'P'
(C) 'T'
(D) 'E'
Correct Answer: (C)
[EN] Negative indices count from the end: -1='R', -2='E', -3='T'.
[HI] ऋणात्मक इंडेक्स अंत से गिने जाते हैं: -1='R', -2='E', -3='T'।
Q2. What is the output of: s = 'PYTHON'; print(s[1:4])?
(s = 'PYTHON'; print(s[1:4]) का आउटपुट क्या होगा?)
(A) PYT
(B) YTH
(C) PYTH
(D) YTHO
Correct Answer: (B)
[EN] Slicing includes index 1 ('Y'), 2 ('T'), and 3 ('H'), stopping before index 4 ('O').
[HI] स्लाइसिंग में इंडेक्स 1 ('Y'), 2 ('T') और 3 ('H') शामिल होते हैं और 4 से ठीक पहले रुक जाता है।
Q3. What happens when Python executes: 'ABC'[10] vs 'ABC'[10:20]?
(पायथन में 'ABC'[10] और 'ABC'[10:20] चलाने पर क्रमशः क्या होता है?)
(A) '' and ''
(B) None and ''
(C) IndexError and '' (empty string)
(D) IndexError and IndexError
Correct Answer: (C)
[EN] Single-element indexing raises IndexError out of bounds, whereas slicing silently clamps bounds and returns ''.
[HI] एकल इंडेक्सिंग सीमा बाहर होने पर IndexError देती है, जबकि स्लाइसिंग बिना एरर के '' लौटाती है।
Q4. What is the output of: s = 'ABCDEFG'; print(s[::2])?
(s = 'ABCDEFG'; print(s[::2]) का आउटपुट क्या होगा?)
(A) ACEG
(B) BDF
(C) ABCDEFG
(D) GECA
Correct Answer: (A)
[EN] With start=0, stop=7, and step=2, Python selects indices 0 ('A'), 2 ('C'), 4 ('E'), and 6 ('G').
[HI] step=2 के साथ इंडेक्स 0 ('A'), 2 ('C'), 4 ('E') और 6 ('G') चुने जाते हैं, जिससे ACEG बनता है।
Q5. What does 'PYTHON'[::-1] return?
('PYTHON'[::-1] क्या मान लौटाता है?)
(A) ''
(B) 'PYTHON'
(C) 'P'
(D) 'NOHTYP'
Correct Answer: (D)
[EN] Step -1 with omitted start and stop traverses the string from the last character backward to index 0.
[HI] step=-1 होने पर स्लाइस अंतिम अक्षर से शुरू होकर उल्टे क्रम में 'NOHTYP' लौटाता है।
Q6. Which error is raised by 'PYTHON'[::0]?
('PYTHON'[::0] चलाने पर कौन सा एरर आता है?)
(A) IndexError
(B) ValueError
(C) TypeError
(D) ZeroDivisionError
Correct Answer: (B)
[EN] A slice step of 0 cannot make progress and always raises ValueError: slice step cannot be zero.
[HI] स्लाइस स्टेप 0 होने पर ValueError: slice step cannot be zero उत्पन्न होता है।
Q7. What is the output of: s = 'RAJASTHAN'; print(s[1:-1])?
(s = 'RAJASTHAN'; print(s[1:-1]) का आउटपुट क्या होगा?)
(A) AJASTHA
(B) AJASTHAN
(C) RAJASTHA
(D) ''
Correct Answer: (A)
[EN] Index 1 is 'A' and index -1 is the final 'N' (excluded as stop), yielding 'AJASTHA'.
[HI] इंडेक्स 1 ('A') से शुरू होकर -1 ('N', शामिल नहीं) से ठीक पहले तक लेने पर 'AJASTHA' मिलता है।
Q8. What is the output of: s = '0123456'; print(s[5:2:-1])?
(s = '0123456'; print(s[5:2:-1]) का आउटपुट क्या होगा?)
(A) 543
(B) ''
(C) 2345
(D) 5432
Correct Answer: (A)
[EN] Starting at 5 and stepping -1 while > 2 selects indices 5, 4, and 3: '543'.
[HI] इंडेक्स 5 से -1 कदम पीछे चलते हुए 2 से पहले रुकने पर इंडेक्स 5, 4, 3 अर्थात '543' प्राप्त होता है।
Q9. What does 'PYTHON'[4:1] evaluate to?
('PYTHON'[4:1] का परिणाम क्या होगा?)
(A) '' (empty string)
(B) IndexError
(C) 'HTY'
(D) 'OHT'
Correct Answer: (A)
[EN] Default step is +1 (left-to-right), but start (4) is already past stop (1), so the slice is empty ''.
[HI] डिफ़ॉल्ट step=+1 (बाएँ से दाएँ) है, लेकिन start (4) पहले ही stop (1) से आगे है, अतः परिणाम '' होगा।
Q10. What is the output of: s = 'INFORMATICS'; print(s[-4:-1])?
(s = 'INFORMATICS'; print(s[-4:-1]) का आउटपुट क्या होगा?)
(A) ATIC
(B) TIC
(C) TICS
(D) CIT
Correct Answer: (B)
[EN] In 'INFORMATICS' (len 11), -4 is 'T' (index 7) and -1 is 'S' (index 10, excluded), so indices 7, 8, 9 give 'TIC'.
[HI] -4 ('T') से शुरू होकर -1 ('S', अपवर्जित) से पहले तक लेने पर 'TIC' प्राप्त होता है।
Q11. What does 'HELLO'[1.5] raise in Python?
(पायथन में 'HELLO'[1.5] चलाने पर कौन सा एरर आता है?)
(A) ValueError
(B) IndexError
(C) TypeError
(D) Truncates to 'E'
Correct Answer: (C)
[EN] String indices must be integers or slices (or objects with __index__), never floats.
[HI] स्ट्रिंग इंडेक्स हमेशा int या slice होना चाहिए; float देने पर TypeError आता है।
Q12. Which expression ALWAYS reconstructs the original string s for any integer index i?
(किसी भी पूर्णांक i के लिए कौन सा व्यंजक हमेशा मूल स्ट्रिंग s को पुनः बनाता है?)
(A) s[0:i-1] + s[i:]
(B) s[::-i] + s[::i]
(C) s[:i] + s[i:]
(D) s[:i] + s[i+1:]
Correct Answer: (C)
[EN] Because s[:i] stops just before i and s[i:] starts at i, s[:i] + s[i:] always equals s.
[HI] चूंकि s[:i] इंडेक्स i से ठीक पहले रुकता है और s[i:] इंडेक्स i से शुरू होता है, इसलिए s[:i] + s[i:] == s होता है।
Q13. What is the output of: s = 'ABCDEF'; print(s[-1:1:-2])?
(s = 'ABCDEF'; print(s[-1:1:-2]) का आउटपुट क्या होगा?)
(A) FDB
(B) EC
(C) DF
(D) FD
Correct Answer: (D)
[EN] Start=-1 (index 5, 'F'), stop=1 ('B', excluded), step=-2: visits index 5 ('F') and index 3 ('D'), stopping before 1.
[HI] start=-1 (इंडेक्स 5, 'F'), stop=1 ('B' अपवर्जित), step=-2 से इंडेक्स 5 ('F') और 3 ('D') चुने जाते हैं -> 'FD'।
Q14. Which built-in type/function lets you store slicing bounds in a variable like sl = ...(1, 5, 2)?
(किस बिल्ट-इन फंक्शन से स्लाइस की सीमाओं को वेरिएबल sl = ...(1, 5, 2) में सेव कर सकते हैं?)
(A) substr(1, 5, 2)
(B) split(1, 5, 2)
(C) slice(1, 5, 2)
(D) range(1, 5, 2)
Correct Answer: (C)
[EN] slice(start, stop, step) constructs a reusable slice object that can be passed inside s[sl].
[HI] slice(start, stop, step) एक पुनः उपयोग योग्य स्लाइस ऑब्जेक्ट बनाता है जिसे s[sl] में पास किया जा सकता है।
Q15. What is the output of: s = 'DATA'; print(s[len(s) - 1], s[-len(s)])?
(s = 'DATA'; print(s[len(s) - 1], s[-len(s)]) का आउटपुट क्या होगा?)
(A) A D
(B) IndexError
(C) D A
(D) T A
Correct Answer: (A)
[EN] len('DATA') is 4; s[3] is the last character 'A', and s[-4] is the first character 'D'.
[HI] len('DATA') = 4 है; s[3] अंतिम अक्षर 'A' और s[-4] पहला अक्षर 'D' है।
Q16. Which of the following statements about Python string indexing and slicing is NOT true?
(पायथन स्ट्रिंग इंडेक्सिंग और स्लाइसिंग के बारे में कौन सा कथन सत्य नहीं है?)
(A) Passing step=0 in a slice raises ValueError
(B) Negative indices access characters starting from -1 at the end of the string
(C) Slicing with out-of-range bounds never raises IndexError
(D) In s[start:stop], the character at index stop is included in the returned substring
Correct Answer: (D)
[EN] Python slices follow half-open interval semantics [start, stop): start is inclusive, whereas stop is strictly exclusive.
[HI] पायथन स्लाइसिंग [start, stop) के नियम का पालन करती है: start शामिल होता है जबकि stop को परिणाम में शामिल नहीं किया जाता।
Q17. Within Python String Indexing (Positive & Negative) and Slicing [start:stop:step], which index / slice form directly maps to: "'N' / 'P'"?
(Python String Indexing (Positive & Negative) and Slicing [start:stop:step] में कौन सा Index / Slice Form सीधे "'N' / 'P'" से संबंधित है?)
(A) s[-1] / s[-6]
(B) s[::-1]
(C) s[1:4]
(D) s[2:99]
Correct Answer: (A)
[EN] In Python String Indexing (Positive & Negative) and Slicing [start:stop:step], 's[-1] / s[-6]' handles ''N' / 'P'' (Negative index maps to len(s) + idx).
[HI] Python String Indexing (Positive & Negative) and Slicing [start:stop:step] में 's[-1] / s[-6]' का कार्य/विवरण ''N' / 'P'' (Negative index maps to len(s) + idx) है।
Q18. Which option accurately describes 's[1:4]' in Python String Indexing (Positive & Negative) and Slicing [start:stop:step]?
(Python String Indexing (Positive & Negative) and Slicing [start:stop:step] के संदर्भ में 's[1:4]' की सही विशेषता कौन सी है?)
(A) 'N' / 'P'
(B) 'YTH'
(C) 'THON'
(D) 'NOHTYP'
Correct Answer: (B)
[EN] 's[1:4]' provides ''YTH'' — key point: Start inclusive, stop index 4 excluded.
[HI] 's[1:4]' का मुख्य विवरण ''YTH'' है (Start inclusive, stop index 4 excluded)।
Q19. In Python String Indexing (Positive & Negative) and Slicing [start:stop:step], identify the correct parameter for the specification: "'NOHTYP'".
(Python String Indexing (Positive & Negative) and Slicing [start:stop:step] में "'NOHTYP'" विवरण किस विकल्प के लिए सही है?)
(A) s[-1] / s[-6]
(B) s[1:4]
(C) s[::-1]
(D) s[2:99]
Correct Answer: (C)
[EN] 's[::-1]' corresponds to ''NOHTYP'' (Negative step reverses entire string).
[HI] 's[::-1]' का संबंध ''NOHTYP'' (Negative step reverses entire string) से है।
Q20. Considering Python String Indexing (Positive & Negative) and Slicing [start:stop:step], what key behavior defines 's[2:99]'?
(Python String Indexing (Positive & Negative) and Slicing [start:stop:step] में 's[2:99]' की मुख्य भूमिका या मान क्या है?)
(A) 'YTH'
(B) 'NOHTYP'
(C) 'N' / 'P'
(D) 'THON'
Correct Answer: (D)
[EN] 's[2:99]' represents ''THON'' (Out-of-range slice clamps; no IndexError).
[HI] 's[2:99]' का अर्थ/मान ''THON'' है (Out-of-range slice clamps; no IndexError)।
Python String Indexing (Positive & Negative) and Slicing [start:stop:step] — Exam FAQs
Q1: Why is the stop index excluded in Python slicing s[start:stop]?
पायथन स्लाइसिंग s[start:stop] में stop इंडेक्स को शामिल क्यों नहीं किया जाता?
[EN] Half-open [start, stop) slicing ensures len(s[a:b]) == b - a (for valid bounds) and guarantees s[:i] + s[i:] == s without overlapping or skipping characters.
[HI] अर्ध-खुले अंतराल के कारण सबस्ट्रिंग की लंबाई सीधे b - a होती है और s[:i] + s[i:] बिना किसी अक्षर को छोड़े या दोहराए पूरी स्ट्रिंग s बनाती है।
Q2: How does s[-2:] work vs s[:-2]?
s[-2:] और s[:-2] में क्या अंतर है?
[EN] s[-2:] returns the last 2 characters of s (starting at -2 to the end), whereas s[:-2] returns everything EXCEPT the last 2 characters (from index 0 up to -2).
[HI] s[-2:] स्ट्रिंग के अंतिम 2 अक्षर देता है, जबकि s[:-2] अंतिम 2 अक्षरों को छोड़कर बाकी पूरी स्ट्रिंग लौटाता है।
Q3: Why does 'abc'[1:2] return 'b' instead of raising an error while 'abc'[5] fails?
'abc'[5] एरर देता है जबकि 'abc'[5:10] खाली स्ट्रिंग क्यों लौटाता है?
[EN] Indexing asks for a single guaranteed position (which cannot exist if out of range, hence IndexError), while slicing asks for all characters overlapping an interval, which can validly be empty.
[HI] इंडेक्सिंग एक निश्चित स्थान का अक्षर माँगती है (जो न होने पर IndexError देती है), जबकि स्लाइसिंग एक अंतराल में आने वाले अक्षर माँगती है जो खाली भी हो सकता है।
Post a Comment
Post a Comment