FliQi Education App Download
प्रश्न-बैंक · Python

String — प्रश्न और उत्तर

Python · 21 जाँचे हुए (verified) प्रश्न

  • 21 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 11 के उत्तर और हल FliQi Question Bank app में।

भाषा
प्रश्न 1उत्तर + हल
स्ट्रिंग s = "Hello" से index 3 वाला अक्षर पाने के लिए कौन-सा command चलाएँगे?
To retrieve the character at index 3 from string s = "Hello", which command do we execute?
  1. As[]
  2. Bs.getitem(3)
  3. Cs.__getitem__(3)
  4. Ds.getItem(3)

सही उत्तर / Answer: (C)

हल / Solution
जब हम s[3] लिखते हैं, तो Python अंदर से s.__getitem__(3) को ही बुलाता है। इसलिए s.__getitem__(3) सीधे चलाने पर भी index 3 का अक्षर मिलता है। "Hello" में index 3 पर l है। s[] में index लिखा ही नहीं, इसलिए यह SyntaxError देता है। getitem और getItem नाम के कोई method string में नहीं हैं, इसलिए ये AttributeError देते हैं। Python के ऐसे ख़ास method के नाम के दोनों तरफ़ दो underscore होते हैं।
When we write s[3], Python internally calls s.__getitem__(3). So running s.__getitem__(3) directly also returns the character at index 3. In "Hello", index 3 holds l. The option s[] has no index inside the brackets, so it is a SyntaxError. Strings have no methods named getitem or getItem, so both of those raise an AttributeError. Special methods like this always have two underscores on each side of the name, which the wrong options leave out.
प्रश्न 2उत्तर + हल
"Hello".replace("l", "e") का नतीजा क्या होगा?
What is "Hello".replace("l", "e")?
  1. AHeeeo
  2. BHeelo
  3. CHeleo
  4. DNone

सही उत्तर / Answer: (A)

हल / Solution
replace(old, new) string में old के हर मिलान को new से बदलकर नई string लौटाता है। "Hello" में l दो बार है, index 2 और 3 पर। दोनों l बदलकर e बन जाते हैं, इसलिए H, e, e, e, o मिलकर "Heeeo" बनता है। "Heelo" में सिर्फ़ पहला l बदला है, जो replace("l", "e", 1) देता। "Heleo" में सिर्फ़ दूसरा बदला है। replace() नई string देता है, None नहीं।
The replace(old, new) method returns a new string in which every occurrence of old is replaced by new. "Hello" has two l characters, at index 2 and 3. Both become e, so H, e, e, e, o give "Heeeo". "Heelo" changes only the first l, which replace("l", "e", 1) would do. "Heleo" changes only the second l. Since replace() always returns a new string, the result is never None.
प्रश्न 3उत्तर + हल
print(ord('b') - ord('a')) द्वारा क्या प्रदर्शित किया जाएगा?
What will be displayed by print(ord('b') - ord('a'))?
  1. A0
  2. B1
  3. C-1
  4. D2

सही उत्तर / Answer: (B)

हल / Solution
ord() किसी अक्षर का Unicode नंबर देता है। छोटे अक्षर a से z तक लगातार नंबर पर होते हैं। ord('a') का मान 97 है और ord('b') का 98। इसलिए 98 − 97 = 1 छपता है। 0 तब आता जब दोनों अक्षर एक ही होते। -1 तब आता जब घटाने का क्रम उलटा होता, यानी ord('a') − ord('b')। 2 तब आता जब b की जगह c होता, क्योंकि c का नंबर 99 है।
The ord() function gives the Unicode number of a character. Lowercase letters a to z have consecutive numbers. The value of ord('a') is 97 and ord('b') is 98. So 98 minus 97 gives 1, which is printed. The value 0 would appear only if both letters were the same. The value -1 would come from the reverse order, ord('a') - ord('b'). The value 2 would need c instead of b, because c has code 99.
प्रश्न 4उत्तर + हल
"hello"+1+2+3 का आउटपुट क्या होगा?
What will be the output of "hello"+1+2+3?
  1. Ahello123
  2. Bhello
  3. CError
  4. Dhello6

सही उत्तर / Answer: (C)

हल / Solution
यह expression बाईं से दाईं ओर चलता है, इसलिए सबसे पहले "hello" + 1 बनता है। Python string और int को अपने-आप नहीं जोड़ता। इसलिए यहीं TypeError आ जाता है और आगे का हिस्सा चलता ही नहीं। "hello123" तभी बनता जब अंकों को str() से string बनाया जाता। "hello6" के लिए 1+2+3 को पहले जोड़कर str() करना पड़ता। सिर्फ़ "hello" कभी नहीं बचता, क्योंकि error के बाद कोई मान नहीं मिलता।
The expression is evaluated from left to right, so the first step is "hello" + 1. Python does not join a string and an int automatically. So a TypeError is raised right there, and the rest of the expression never runs. "hello123" would need each number converted with str() first. "hello6" would need 1+2+3 added first and then converted with str(). Plain "hello" is never produced, because an error gives no value at all.
प्रश्न 5उत्तर + हल
मान लीजिए s = "\t\tWorld\n" है। s.strip() क्या लौटाएगा?
Suppose s is "\t\tWorld\n". What is s.strip()?
  1. A\t\tWorld\n
  2. BWorld\n
  3. C\t\tWORLD\n
  4. DWorld

सही उत्तर / Answer: (D)

हल / Solution
strip() string के दोनों सिरों से whitespace हटाता है। whitespace में space, tab (\t) और newline (\n) तीनों आते हैं। यहाँ शुरू में दो tab हैं और आख़िर में एक newline। strip() दोनों तरफ़ से ये हटा देता है, इसलिए सिर्फ़ "World" बचता है। विकल्प (A) बिना बदली string है। विकल्प (B) lstrip() का नतीजा है, जो सिर्फ़ बाईं ओर साफ़ करता है। strip() अक्षरों को बड़ा नहीं करता, इसलिए (C) ग़लत है।
The strip() method removes whitespace from both ends of a string. Whitespace includes spaces, tabs (\t) and newlines (\n). Here the string starts with two tabs and ends with one newline. strip() removes them from both sides, leaving only "World". Option (A) is the unchanged string. Option (B) is what lstrip() would return, since it cleans only the left side. strip() never changes letter case, so option (C) with WORLD in capitals is also wrong.
प्रश्न 6उत्तर + हल
निम्नलिखित Python स्टेटमेंट का आउटपुट क्या होगा?
>>>print(chr(ord('b')+1))
What will be the output of the following Python statement?
>>>print(chr(ord('b')+1))
  1. Aa
  2. Bb
  3. Cc
  4. DA

सही उत्तर / Answer: (C)

हल / Solution
ord('b') छोटे b का Unicode नंबर देता है, जो 98 है। उसमें 1 जोड़ने पर 99 बनता है। chr(99) नंबर 99 वाला अक्षर लौटाता है, जो c है। यानी यह तरीक़ा वर्णमाला में अगला अक्षर देता है। a तब आता जब 1 घटाया जाता। b तब रहता जब कुछ जोड़ा ही न जाता। बड़ा A का नंबर 65 है, जो 99 से बहुत दूर है, इसलिए वह भी ग़लत है।
The call ord('b') gives the Unicode number of lowercase b, which is 98. Adding 1 gives 99. Then chr(99) returns the character with code 99, which is c. In other words, this pattern gives the next letter of the alphabet. The letter a would come if 1 were subtracted instead. The letter b would remain only if nothing were added. Capital A has code 65, which is far from 99, so it is wrong as well.
प्रश्न 7उत्तर + हल
निम्नलिखित Python स्टेटमेंट का आउटपुट क्या होगा?
>>>chr(ord('A'))
What will be the output of the following Python statement?
>>>chr(ord('A'))
  1. AA
  2. BB
  3. Ca
  4. DError

सही उत्तर / Answer: (A)

हल / Solution
ord() किसी अक्षर का Unicode नंबर देता है, और chr() नंबर से वापस अक्षर बना देता है। यानी दोनों एक-दूसरे का उलटा काम करते हैं। ord('A') का मान 65 है। फिर chr(65) वापस 'A' देता है। इसलिए नतीजा A ही रहता है। B तब आता जब नंबर में 1 जोड़ा जाता, जैसे chr(ord('A')+1)। छोटा a का नंबर 97 है, जो यहाँ बनता ही नहीं। दोनों function सही हैं, कोई Error नहीं।
The ord() function gives the Unicode number of a character, and chr() turns a number back into a character. So each one undoes the other. The value of ord('A') is 65, and chr(65) gives back 'A'. So the result is A itself. B would appear only if 1 were added, as in chr(ord('A')+1). The lowercase a has code 97, which is never produced here. Both calls are valid, so there is no error in this statement.
प्रश्न 8उत्तर + हल
निम्नलिखित Python कोड का आउटपुट क्या होगा?
>>>example="helloworld"
>>>example[::-1].startswith("d")
What will be the output of the following Python code?
>>>example="helloworld"
>>>example[::-1].startswith("d")
  1. Adlrowolleh
  2. BTrue
  3. C-1
  4. DNone

सही उत्तर / Answer: (B)

हल / Solution
यहाँ दो काम एक के बाद एक होते हैं। पहले [::-1] string को उलटकर "dlrowolleh" बनाता है। फिर startswith("d") देखता है कि यह नई string d से शुरू होती है या नहीं। उलटी string का पहला अक्षर d है, इसलिए नतीजा True है। startswith() हमेशा True या False लौटाता है। इसलिए "dlrowolleh" सिर्फ़ बीच का नतीजा है, आख़िरी नहीं। -1 find() जैसे method देते हैं और None यहाँ कभी नहीं आता।
Two steps happen here, one after the other. First, [::-1] reverses the string to "dlrowolleh". Then startswith("d") checks whether this new string begins with d. The reversed string starts with d, so the result is True. The startswith() method always returns a boolean, True or False. So "dlrowolleh" is only the intermediate value, not the final output. A value like -1 comes from methods such as find(), and None is never returned here.
प्रश्न 9उत्तर + हल
निम्नलिखित Python कोड का आउटपुट क्या होगा?
>>>example = "helle"
>>>example.rfind("e")
What will be the output of the following Python code?
>>>example = "helle"
>>>example.rfind("e")
  1. A-1
  2. B4
  3. C3
  4. D1

सही उत्तर / Answer: (B)

हल / Solution
rfind() में r का मतलब right है। यह दाईं ओर से देखकर आख़िरी मिलान का index लौटाता है, पर index गिनती बाईं ओर से ही होती है। "helle" में e दो जगह है, index 1 और index 4 पर। आख़िरी e index 4 पर है, इसलिए नतीजा 4 है। 1 पहला मिलान है, जो find() देता। index 3 पर l है, e नहीं। -1 तब आता जब e मिलता ही नहीं।
The r in rfind() stands for right. It returns the index of the last match, but the index itself is still counted from the left. In "helle", the letter e appears at index 1 and index 4. The last e is at index 4, so the result is 4. The value 1 is the first match, which find() would return. Index 3 holds l, not e. The value -1 means not found.
प्रश्न 10उत्तर + हल
निम्नलिखित Python कोड का आउटपुट क्या होगा?
>>>example = "helle"
>>>example.find("e")
What will be the output of the following Python code?
>>>example = "helle"
>>>example.find("e")
  1. AError
  2. B-1
  3. C1
  4. D0

सही उत्तर / Answer: (C)

हल / Solution
find() method बाईं ओर से खोजता है और पहले मिलान का index लौटाता है। "helle" में index 0 पर h, 1 पर e, 2 पर l, 3 पर l और 4 पर e है। e पहली बार index 1 पर मिलता है, इसलिए नतीजा 1 है। index 4 वाला e दूसरा मिलान है, जिसे rfind() देता। -1 तब आता जब e मिलता ही नहीं। 0 पर h है, e नहीं। find() कभी Error नहीं देता।
The find() method searches from the left and returns the index of the first match. In "helle", index 0 is h, 1 is e, 2 is l, 3 is l and 4 is e. The letter e first appears at index 1, so the result is 1. The e at index 4 is the second match, which rfind() would return. The value -1 appears only when nothing is found. Index 0 holds h, and find() never raises an error.
आगे के 11 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 16उत्तर app में
निम्नलिखित Python कोड का आउटपुट क्या होगा?
>>>print(r"\nhello")
What will be the output of the following Python code?
>>>print(r"\nhello")
  1. Aएक नई पंक्ति और फिर helloa new line and hello
  2. B\nhello
  3. Cअक्षर r और फिर hellothe letter r and then hello
  4. DError
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
String के सभी 21 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

Python के दूसरे topics

सभी topics