PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Strings: Creation, Quotes (Single, Double, Triple) (पायथन स्ट्रिंग्स: निर्माण, ट्रिपल कोट्स (मल्टी-लाइन) और इम्यूटेबिलिटी) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python strings (str) are immutable sequences of Unicode code points created using single ('...'), double ("..."), or triple ('''...''' / """...""") quotes, with no separate char type. Raw strings (r'...') suppress escape-sequence processing for regexes and Windows paths, while triple quotes preserve multi-line newlines and serve as PEP 257 docstrings.
अध्याय सार: पायथन स्ट्रिंग्स (str) यूनिकोड कोड पॉइंट्स के इम्यूटेबल सीक्वेंस हैं जिन्हें सिंगल ('...'), डबल ("...") या ट्रिपल ('''...''' / """...""") कोट्स से बनाया जाता है और इनमें अलग char डेटा टाइप नहीं होता। रॉ स्ट्रिंग्स (r'...') एस्केप सीक्वेंस को निष्क्रिय रखती हैं, जबकि ट्रिपल कोट्स मल्टी-लाइन टेक्स्ट और डॉकस्ट्रिंग (docstrings) लिखने के काम आते हैं।
Python Strings: Creation, Quotes (Single, Double, Triple): Detailed Notes & Architecture (पायथन स्ट्रिंग्स: निर्माण, ट्रिपल कोट्स (मल्टी-लाइन) और इम्यूटेबिलिटी)
1. Unicode String Model and Quote Styles (Single vs Double) (यूनिकोड स्ट्रिंग मॉडल और कोट्स के प्रकार (सिंगल बनाम डबल))
[EN] In Python 3, all str literals store Unicode (UTF-8 source encoding by default) rather than legacy ASCII bytes, and there is no separate char type—a single letter like 'A' is simply a str of length 1. Single quotes ('Python') and double quotes ("Python") produce identical single-line str objects; developers choose one mainly to embed the opposite quote cleanly without backslashes (e.g., "It's valid"). Unescaped newlines inside single or double quotes cause SyntaxError: unterminated string literal unless continued with a trailing backslash or enclosed in parentheses for implicit compile-time concatenation.
[HI] पायथन 3 में सभी str लिटरल यूनिकोड अक्षरों को स्टोर करते हैं और इनमें C/Java की तरह अलग char टाइप नहीं होता—'A' भी लंबाई 1 की एक str ही है। सिंगल ('Python') और डबल ("Python") कोट्स बिल्कुल समान सिंगल-लाइन स्ट्रिंग बनाते हैं; इनका चुनाव अंदर दूसरे प्रकार के कोट्स को बिना बैकस्लैश लिखने की सुविधा देता है। सिंगल या डबल कोट्स में सीधे नई लाइन (Enter) देने पर SyntaxError आता है, जब तक कि कोष्ठक के भीतर इम्प्लिसिट कॉनकैटेनेशन या लाइन-कंटिन्यूएशन का उपयोग न हो।
2. Triple Quotes, Multi-Line Strings, and Docstrings (ट्रिपल कोट्स, मल्टी-लाइन स्ट्रिंग्स और डॉकस्ट्रिंग्स)
[EN] Triple single quotes ('''...''') and triple double quotes ("""...""") span multiple lines freely while preserving embedded newlines (\n), tabs, and unescaped single or double quotes inside the text. When a string literal appears as the very first statement of a module, class, or function, Python attaches it to the object's __doc__ attribute as a docstring (PEP 257 strongly recommends """...""" for docstrings). Unlike # comments which the parser discards completely, standalone string literals outside assignments are parsed as expressions (or stored in __doc__), though unreferenced strings are dropped from bytecode.
[HI] ट्रिपल सिंगल ('''...''') और ट्रिपल डबल ("""...""") कोट्स कई पंक्तियों में फैले टेक्स्ट को वास्तविक न्यूलाइन (\n) सहित सुरक्षित रखते हैं। जब कोई स्ट्रिंग किसी मॉड्यूल, क्लास या फंक्शन के पहले स्टेटमेंट के रूप में लिखी जाती है, तो पायथन उसे __doc__ एट्रिब्यूट में डॉकस्ट्रिंग के रूप में सहेजता है (PEP 257 में """...""" अनुशंसित है)। # टिप्पणियों के विपरीत, डॉकस्ट्रिंग को help() और इंट्रोस्पेक्शन टूल्स द्वारा रनटाइम पर पढ़ा जा सकता है।
3. Escape Sequences and Raw Strings (r'...') (एस्केप सीक्वेंस और रॉ स्ट्रिंग्स (r'...'))
[EN] Inside standard strings, the backslash (\) starts escape sequences such as \n (newline), \t (tab), \\ (literal backslash), \xHH (hex byte), and \uHHHH (Unicode code point). Prefixing a string with r or R (e.g., r'C:\new\test') creates a raw string where backslashes remain literal characters—essential for Windows file paths and regular expressions. Crucial edge case: even a raw string cannot end with an odd number of backslashes (e.g., r'C:\' raises SyntaxError) because the trailing backslash escapes the closing quote during tokenization.
[HI] सामान्य स्ट्रिंग्स में बैकस्लैश (\) एस्केप सीक्वेंस जैसे \n (नई लाइन), \t (टैब), \\ (शाब्दिक बैकस्लैश) और \uHHHH (यूनिकोड) को दर्शाता है। स्ट्रिंग से पहले r या R लगाने पर (जैसे r'C:\new\test') रॉ स्ट्रिंग बनती है जिसमें बैकस्लैश सामान्य अक्षर की तरह रहता है—यह विंडोज पथ और रेगेक्स के लिए बेहद उपयोगी है। एक महत्वपूर्ण अपवाद: कोई भी रॉ स्ट्रिंग विषम संख्या के बैकस्लैश पर समाप्त नहीं हो सकती (जैसे r'C:\' में SyntaxError आता है) क्योंकि अंतिम बैकस्लैश क्लोजिंग कोट को एस्केप कर देता है।
4. Immutability, Operators (+, *), and str() Conversion (इम्यूटेबिलिटी, ऑपरेटर्स (+, *) और str() रूपांतरण)
[EN] Python strings are strictly immutable: once constructed, individual characters cannot be altered in-place, so executing s[0] = 'X' raises TypeError: 'str' object does not support item assignment. Every modification, concatenation (s1 + s2), or repetition (s * 3) allocates a brand-new str object (note: 'a' + 5 raises TypeError without explicit str(5), and 'a' * -2 returns ''). Immutability guarantees thread safety, enables CPython string interning, and allows strings to be hashed as dictionary keys and set elements.
[HI] पायथन स्ट्रिंग्स पूर्णतः इम्यूटेबल (अपरिवर्तनीय) होती हैं: निर्माण के बाद s[0] = 'X' द्वारा किसी अक्षर को बदलना संभव नहीं है, ऐसा करने पर TypeError आता है। प्रत्येक जोड़ (+) या दोहराव (*) हमेशा एक नया स्ट्रिंग ऑब्जेक्ट बनाता है ('a' + 5 से TypeError आता है जब तक str(5) न लिखा जाए, और 'a' * -2 खाली स्ट्रिंग '' देता है)। इम्यूटेबिलिटी के कारण ही स्ट्रिंग्स हैशेबल होती हैं और डिक्शनरी कुंजी या सेट के तत्व के रूप में उपयोग की जा सकती हैं।
Syntax, Formula & Technical Example (Python Strings: Creation, Quotes (Single, Double, Triple))
s1 = 'Single quote'
s2 = "It's double"
s3 = '''Line 1
Line 2'''
raw_path = r'C:\Users\new_folder'
print(type('A'), len('A')) # <class 'str'> 1
print(s2, 'Ha' * 3) # It's double HaHaHa
print(len(raw_path), s3.count('\n')) # 19 1
Python Strings: Creation, Quotes (Single, Double, Triple) Quick Reference & Comparison Table
| String Literal / Feature | Syntax & Behavior | Key Exam Fact |
|---|---|---|
| Single / Double Quotes | 'abc' or "abc" (1-line str) | Identical type; no char type in Python |
| Triple Quotes | '''...''' or """...""" (multi-line) | Preserves newlines; PEP 257 docstrings |
| Raw String | r'C:\new\file' (ignores escapes) | Cannot end with a single trailing \ |
| Immutability | Characters cannot change in-place | s[0] = 'x' raises TypeError; hashable |
Python Strings: Creation, Quotes (Single, Double, Triple) Top 20 Bilingual MCQs with Answers
Q1. What is the return value of type('A') in Python 3?
(पायथन 3 में type('A') का मान क्या होता है?)
(A) <class 'char'>
(B) <class 'chr'>
(C) <class 'str'>
(D) <class 'unicode'>
Correct Answer: (C)
[EN] Python has no separate character data type; a single character is simply a str object of length 1.
[HI] पायथन में अलग char डेटा टाइप नहीं होता; एक अक्षर भी लंबाई 1 का str ऑब्जेक्ट ही होता है।
Q2. What happens when you execute s = 'hello'; s[0] = 'H'?
(s = 'hello'; s[0] = 'H' चलाने पर क्या होता है?)
(A) Raises TypeError
(B) Raises ValueError
(C) Raises IndexError
(D) Changes s to 'Hello'
Correct Answer: (A)
[EN] Python strings are immutable, so item assignment on a str raises TypeError: 'str' object does not support item assignment.
[HI] पायथन स्ट्रिंग्स इम्यूटेबल होती हैं, इसलिए आइटम असाइनमेंट करने पर TypeError आता है।
Q3. What is the length of the raw string r'\n\t'?
(रॉ स्ट्रिंग r'\n\t' की लंबाई (len) कितनी होगी?)
(A) 2
(B) 5
(C) 3
(D) 4
Correct Answer: (D)
[EN] The r prefix disables escape sequences, so \, n, \, t are four separate literal characters.
[HI] r प्रीफिक्स एस्केप सीक्वेंस को बंद कर देता है, इसलिए \, n, \, t चार अलग अक्षर गिने जाते हैं।
Q4. Which of the following Python string literals causes a SyntaxError?
(निम्नलिखित में से किस पायथन स्ट्रिंग लिटरल से SyntaxError उत्पन्न होगा?)
(A) "It's 'ok'"
(B) '''hello\nworld'''
(C) r'C:\path\\'
(D) r'C:\path\'
Correct Answer: (D)
[EN] A raw string cannot end with an odd number of backslashes because the final backslash escapes the closing quote.
[HI] रॉ स्ट्रिंग विषम संख्या के बैकस्लैश पर समाप्त नहीं हो सकती क्योंकि अंतिम \ क्लोजिंग कोट को एस्केप कर देता है।
Q5. What is the output of: print(('Py' 'thon') * 2)?
(print(('Py' 'thon') * 2) का आउटपुट क्या होगा?)
(A) SyntaxError
(B) PyPy thonthon
(C) TypeError
(D) PythonPython
Correct Answer: (D)
[EN] Adjacent string literals separated by whitespace inside parentheses undergo implicit compile-time concatenation into 'Python', then * 2 repeats it.
[HI] कोष्ठक के भीतर पास-पास लिखे स्ट्रिंग लिटरल स्वतः जुड़कर 'Python' बनते हैं और * 2 से दो बार दोहराए जाते हैं।
Q6. What does 'abc' * 0 and 'abc' * -3 evaluate to?
('abc' * 0 और 'abc' * -3 का मान क्या होता है?)
(A) Raises ValueError for -3
(B) 'abc' * -3 gives 'cba'
(C) Raises TypeError
(D) Both evaluate to '' (empty string)
Correct Answer: (D)
[EN] Multiplying a string by 0 or any negative integer returns the empty string '' without error.
[HI] किसी स्ट्रिंग को 0 या किसी ऋणात्मक पूर्णांक से गुणा करने पर बिना एरर के खाली स्ट्रिंग '' प्राप्त होती है।
Q7. Which attribute stores a function's triple-quoted documentation string?
(किसी फंक्शन की ट्रिपल-कोटेड डॉकस्ट्रिंग किस एट्रिब्यूट में संग्रहीत होती है?)
(A) __str__
(B) __info__
(C) __help__
(D) __doc__
Correct Answer: (D)
[EN] Python automatically binds the first string literal inside a module, class, or function to its __doc__ attribute.
[HI] पायथन मॉड्यूल, क्लास या फंक्शन के पहले स्ट्रिंग लिटरल को स्वतः उसके __doc__ एट्रिब्यूट में सहेजता है।
Q8. What is the output of: print(len('a\nb\tc'))?
(print(len('a\nb\tc')) का आउटपुट क्या होगा?)
(A) 7
(B) 6
(C) 4
(D) 5
Correct Answer: (D)
[EN] \n (newline) and \t (tab) each count as a single escape character, giving 5 characters total: a, newline, b, tab, c.
[HI] \n और \t प्रत्येक 1-1 अक्षर के रूप में गिने जाते हैं, अतः कुल लंबाई 5 (a, न्यूलाइन, b, टैब, c) होगी।
Q9. What happens when executing 'Age: ' + 25 in Python?
(पायथन में 'Age: ' + 25 चलाने पर क्या होता है?)
(A) Prints 'Age: 25'
(B) Raises TypeError
(C) Raises ValueError
(D) Prints 'Age: 25.0'
Correct Answer: (B)
[EN] Unlike Java or JavaScript, Python does not implicitly coerce integers to strings during + concatenation; use str(25).
[HI] Java/JS के विपरीत पायथन + के साथ int को स्वतः str में नहीं बदलता; इसके लिए TypeError आता है।
Q10. Which quote style does PEP 257 recommend for writing docstrings?
(PEP 257 डॉकस्ट्रिंग लिखने के लिए किस कोट स्टाइल की अनुशंसा करता है?)
(A) Raw single quotes (r'...')
(B) Triple single quotes ('''...''')
(C) Triple double quotes ("""...""")
(D) Single quotes ('...')
Correct Answer: (C)
[EN] PEP 257 standardizes triple double quotes ("""...""") for all docstrings, even one-liners.
[HI] PEP 257 एक-पंक्ति और बहु-पंक्ति दोनों डॉकस्ट्रिंग के लिए ट्रिपल डबल कोट्स ("""...""") की अनुशंसा करता है।
Q11. What is the value of bool('') versus bool(' ') in Python?
(पायथन में bool('') और bool(' ') के मान क्रमशः क्या होते हैं?)
(A) True and True
(B) True and False
(C) False and False
(D) False and True
Correct Answer: (D)
[EN] Only the zero-length empty string '' is falsy; ' ' has length 1 (a space character) and is truthy.
[HI] केवल शून्य लंबाई वाली '' स्ट्रिंग फॉल्सी होती है; ' ' में एक स्पेस (लंबाई 1) होने से यह ट्रूथी है।
Q12. What is the output of: print('Line1\\Line2')?
(print('Line1\\Line2') (दो बैकस्लैश लिटरल में) प्रिंट करने पर क्या दिखता है?)
(A) Line1Line2
(B) SyntaxError
(C) Line1\Line2
(D) Line1\\Line2
Correct Answer: (C)
[EN] Inside a non-raw string literal, \\ represents a single literal backslash character.
[HI] सामान्य स्ट्रिंग लिटरल में \\ एक सिंगल बैकस्लैश (\) अक्षर को दर्शाता है।
Q13. How many characters are in the string created by str(True)?
(str(True) द्वारा बनी स्ट्रिंग में कितने अक्षर होते हैं?)
(A) 4
(B) 5
(C) TypeError
(D) 1
Correct Answer: (A)
[EN] str(True) converts the boolean to the 4-character string 'True'.
[HI] str(True) बूलियन मान को 4 अक्षरों की स्ट्रिंग 'True' में बदल देता है।
Q14. Which expression creates an empty string in Python?
(निम्नलिखित में से कौन सा व्यंजक पायथन में एक खाली (empty) स्ट्रिंग बनाता है?)
(A) str()
(B) '' '' == None
(C) str(None)
(D) str(0)
Correct Answer: (A)
[EN] Calling str() with no arguments returns ''; str(None) returns 'None' (length 4) and str(0) returns '0'.
[HI] बिना किसी आर्ग्युमेंट के str() कॉल करने पर '' बनता है; str(None) से 'None' और str(0) से '0' बनता है।
Q15. Why can Python strings safely be used as dictionary keys?
(पायथन स्ट्रिंग्स को डिक्शनरी कुंजी (keys) के रूप में सुरक्षित रूप से क्यों उपयोग किया जा सकता है?)
(A) Because strings are always stored in contiguous C arrays of ASCII bytes
(B) Because str objects are immutable and therefore hashable
(C) Because dict keys only allow str types
(D) Because strings are mutable containers
Correct Answer: (B)
[EN] Dictionary keys must be hashable (constant hash over lifetime), a property guaranteed by str immutability.
[HI] डिक्शनरी की कुंजियों का हैशेबल होना जरूरी है, जो स्ट्रिंग की अपरिवर्तनीयता (immutability) से सुनिश्चित होता है।
Q16. Which of the following statements about Python strings is NOT true?
(पायथन स्ट्रिंग्स के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) Concatenating two strings with + creates a new str object
(B) Single-quoted and double-quoted strings have different internal types in Python
(C) Triple-quoted strings can span multiple lines without using \n
(D) Python 3 strings store Unicode characters natively
Correct Answer: (B)
[EN] Both '...' and "..." create the exact same <class 'str'> type; there is no semantic difference between single and double quotes.
[HI] सिंगल और डबल कोट्स दोनों ही बिल्कुल एक समान <class 'str'> टाइप की स्ट्रिंग बनाते हैं, इनमें कोई आंतरिक अंतर नहीं है।
Q17. Within Python Strings: Creation, Quotes (Single, Double, Triple), which string literal / feature directly maps to: "'abc' or "abc" (1-line str)"?
(Python Strings: Creation, Quotes (Single, Double, Triple) में कौन सा String Literal / Feature सीधे "'abc' or "abc" (1-line str)" से संबंधित है?)
(A) Immutability
(B) Triple Quotes
(C) Single / Double Quotes
(D) Raw String
Correct Answer: (C)
[EN] In Python Strings: Creation, Quotes (Single, Double, Triple), 'Single / Double Quotes' handles ''abc' or "abc" (1-line str)' (Identical type; no char type in Python).
[HI] Python Strings: Creation, Quotes (Single, Double, Triple) में 'Single / Double Quotes' का कार्य/विवरण ''abc' or "abc" (1-line str)' (Identical type; no char type in Python) है।
Q18. Which option accurately describes 'Triple Quotes' in Python Strings: Creation, Quotes (Single, Double, Triple)?
(Python Strings: Creation, Quotes (Single, Double, Triple) के संदर्भ में 'Triple Quotes' की सही विशेषता कौन सी है?)
(A) Characters cannot change in-place
(B) r'C:\new\file' (ignores escapes)
(C) 'abc' or "abc" (1-line str)
(D) '''...''' or """...""" (multi-line)
Correct Answer: (D)
[EN] 'Triple Quotes' provides ''''...''' or """...""" (multi-line)' — key point: Preserves newlines; PEP 257 docstrings.
[HI] 'Triple Quotes' का मुख्य विवरण ''''...''' or """...""" (multi-line)' है (Preserves newlines; PEP 257 docstrings)।
Q19. In Python Strings: Creation, Quotes (Single, Double, Triple), identify the correct parameter for the specification: "r'C:\new\file' (ignores escapes)".
(Python Strings: Creation, Quotes (Single, Double, Triple) में "r'C:\new\file' (ignores escapes)" विवरण किस विकल्प के लिए सही है?)
(A) Single / Double Quotes
(B) Triple Quotes
(C) Raw String
(D) Immutability
Correct Answer: (C)
[EN] 'Raw String' corresponds to 'r'C:\new\file' (ignores escapes)' (Cannot end with a single trailing \).
[HI] 'Raw String' का संबंध 'r'C:\new\file' (ignores escapes)' (Cannot end with a single trailing \) से है।
Q20. Considering Python Strings: Creation, Quotes (Single, Double, Triple), what key behavior defines 'Immutability'?
(Python Strings: Creation, Quotes (Single, Double, Triple) में 'Immutability' की मुख्य भूमिका या मान क्या है?)
(A) Characters cannot change in-place
(B) 'abc' or "abc" (1-line str)
(C) '''...''' or """...""" (multi-line)
(D) r'C:\new\file' (ignores escapes)
Correct Answer: (A)
[EN] 'Immutability' represents 'Characters cannot change in-place' (s[0] = 'x' raises TypeError; hashable).
[HI] 'Immutability' का अर्थ/मान 'Characters cannot change in-place' है (s[0] = 'x' raises TypeError; hashable)।
Python Strings: Creation, Quotes (Single, Double, Triple) — Exam FAQs
Q1: Is there any performance or behavioral difference between single (' ') and double (" ") quotes in Python?
क्या पायथन में सिंगल (' ') और डबल (" ") कोट्स के बीच प्रदर्शन या व्यवहार का कोई अंतर है?
[EN] None at all—unlike C/Java (where 'a' is a char and "a" is a String) or PHP/Bash (variable interpolation), both produce an identical str object in Python.
[HI] बिल्कुल नहीं—C, Java या PHP के विपरीत पायथन में दोनों बिल्कुल समान str ऑब्जेक्ट बनाते हैं।
Q2: Why did r'C:\folder\' cause a SyntaxError even though r means raw string?
r को रॉ स्ट्रिंग कहने के बावजूद r'C:\folder\' में SyntaxError क्यों आता है?
[EN] Python's lexer still allows \' or \" inside raw strings so quotes can be included, which causes a single trailing backslash before the closing quote to escape that quote. Write r'C:\folder' + '\\' or 'C:\\folder\\' instead.
[HI] पायथन का लेक्सर रॉ स्ट्रिंग में भी अंतिम बैकस्लैश को क्लोजिंग कोट एस्केप करने की अनुमति देता है, इसलिए रॉ स्ट्रिंग अकेले \ पर समाप्त नहीं हो सकती।
Q3: How do triple-quoted strings handle indentation inside functions?
फंक्शन के भीतर ट्रिपल-कोटेड स्ट्रिंग लिखने पर इंडेंटेशन कैसे काम करता है?
[EN] Every space on subsequent lines inside triple quotes becomes part of the string verbatim. Use textwrap.dedent() if you want clean code indentation without extra leading spaces in the string.
[HI] ट्रिपल कोट्स के अंदर की सभी अतिरिक्त खाली जगहें (spaces) भी स्ट्रिंग का हिस्सा बन जाती हैं; इन्हें हटाने के लिए textwrap.dedent() का प्रयोग किया जाता है।
Post a Comment
Post a Comment