Q.Select the correct output of the code: S="Amrit Mahotsav @ 75" A=S.partition(" ") print(A)
🔒You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
🔒 Start your 14-day free trial to unlock the full solution →Concept understanding — Python String Slicing and Methods
A string in Python is an immutable sequence of characters that supports indexing and slicing, where s[a:b:c] returns a substring from index a up to but not including b in steps of c, and negative indices count from the end. String methods such as upper(), lower(), strip(), replace(), find(), split(), count() and partition() return new strings or values without changing the original. These operations are used …
The partition() method splits a string at the first occurrence of the specified separator. It returns a 3-tuple containing:
- The part of the string before the separator.
- The separator itself.
- The part of the string after the separator.
In the given code:
S = "Amrit Mahotsav @ 75"A = S.partition(" ")searches for the first space.- The first space is found immediately after "Amrit". …
The partition() method splits a string into three parts at the first occurrence of the specified separator, returning a tuple. The output is ('Amrit', ' ', 'Mahotsav @ 75').
The core concept here revolves around understanding how Python's string partition() method works. Unlike split(), which can divide a string into many parts based on a separator and returns a list, partition() has a very specific job: it always divides the string into exactly three parts and returns them as a tuple.
Here's the breakdown of what those three parts are:
- The part of the string before the first occurrence of the separator.
- The separator itself.
- The part of the string after the first occurrence of the separator.
The key is "first occurrence." If the separator appears multiple times, partition() only considers the very first one it finds. If the separator is not found in the string, partition() returns a tuple containing the original string, followed by two empty strings ('', '').
Let's apply this to the given code.
-
Identify the string and the separator.
We have the string
S = "Amrit Mahotsav @ 75".The
partition()method is called withS.partition(" "), meaning the separator we are looking for is a single space character" ". -
Locate the first occurrence of the separator.
In the string "Amrit Mahotsav @ 75", the first space character appears immediately after the word "Amrit".
S = "Amrit Mahotsav @ 75"^(First space) -
Divide the string into three parts based on the first separator.
- Part 1 (before the separator): Everything before the first space is "Amrit".
- Part 2 (the separator itself): The separator is " ".
- Part 3 (after the separator): Everything after the first space is "Mahotsav @ 75".
-
Form the tuple.
The
partition()method returns these three parts as a tuple.So,
Awill be('Amrit', ' ', 'Mahotsav @ 75'). …
- CBSE 2026Set 91/41 markMCQQ.Identify the output of the following code snippet : s = "the Truth" print(s.capitalize()) (A) The truth (B) THE TRUTH (C) The Truth (D) the Truth
›Reveal solutionSolution
The
capitalize()method converts the first character of a string to uppercase and all other characters to lowercase. For"the Truth", the output is "The truth".The core concept here is understanding how Python's built-in string method
capitalize()operates. String methods in Python are powerful tools for manipulating text data, and each has a specific function.capitalize()is designed for a very particular kind of text formatting: ensuring that a string starts with an uppercase letter and all subsequent letters are lowercase.This method is useful when you want to format text to resemble a proper sentence start, regardless of its initial casing. It doesn't just capitalize the first letter; it also enforces lowercase for the rest of the string, which is a crucial detail often overlooked.
-
Initial String Definition:
We begin with the string variable
sassigned the value"the Truth".s = "the Truth" -
Understanding
s.capitalize():The
capitalize()method is called on the strings. This method performs two distinct actions:- It converts the first character of the string to uppercase.
- It converts all subsequent characters in the string to lowercase.
It's important to remember that string methods in Python, like
capitalize(), do not modify the original strings. Instead, they return a new string with the applied changes.
-
Applying the
capitalize()method:Let's apply these rules to our string
s = "the Truth":- The first character is 't'. According to the rule, this 't' will be converted to 'T'.
- The remaining characters are "he Truth". According to the rule, all letters in this substring will be converted to lowercase. So, 'h' remains 'h', 'e' remains 'e', ' ' remains ' ', 'T' becomes 't', 'r' remains 'r', 'u' remains 'u', 't' remains 't', and 'h' remains 'h'. Combining these, "he Truth" becomes "he truth".
-
Constructing the Resulting String: …
-
- CBSE 2026Set 91/41 markMCQQ.What is the output of the following code snippet ? s='War and Peace by Leo Tolstoy' print(s.partition("by")) (A) ('War and Peace ', 'by', ' Leo Tolstoy') (B) ['War and Peace ', 'by', ' Leo Tolstoy'] (C) ('War and Peace ', ' Leo Tolstoy') (D) ['War and Peace ', ' Leo Tolstoy']
›Reveal solutionSolution
The
partition()method splits a string at the first occurrence of a separator and returns a tuple of three parts: everything before the separator, the separator itself, and everything after. Fors.partition("by"), the output is('War and Peace ', 'by', ' Leo Tolstoy'), which matches option (A).The key here is understanding what
partition()does in Python — and what it returns. Many students confuse it withsplit()or forget that it returns a tuple, not a list.partition()is a string method that searches for a given separator in the string. When it finds the first occurrence, it cuts the string into three pieces: the part before the separator, the separator itself, and the part after. If the separator isn't found, it returns the original string followed by two empty strings. The result is always a tuple of three strings.Let's trace through the code:
-
The string
sis'War and Peace by Leo Tolstoy'. The separator we're looking for is"by". -
Python scans the string from left to right. The first occurrence of
"by"appears after'War and Peace '— note the space before"by"is part of the first segment, not the separator. The separator"by"itself is exactly those two characters. -
Everything before
"by"is'War and Peace '(including the trailing space). The separator itself is'by'. Everything after"by"is' Leo Tolstoy'(starting with a space). -
partition()packages these three strings into a tuple:('War and Peace ', 'by', ' Leo Tolstoy'). …
-
- CBSE 2026Set 91/41 markQ.What will be the output of the following statement ? print("PythonProgram"[-1:2:-2])
›Reveal solutionSolution
This slice uses a negative step to walk the string in reverse, from the last character down to (but not including) index 2, taking every second character. The output is
mroPo.Concept — Python string slicing with a negative step
Python's slice syntax is
[start:stop:step]:- start — the index where the slice begins.
- stop — the index where it ends; the character at
stopis not included. - step — the jump between indices. A negative step walks the string from right to left, continuing while the current index is greater than
stop.
A negative index counts from the end:
-1is the last character. For a string of length 13, index-1is the same as index 12.Step-by-step trace
The string
"PythonProgram"has 13 characters:Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Char P y t h o n P r o g r a m For
[-1:2:-2]:start = -1(that is index 12),stop = 2,step = -2. We move backward from index 12, taking every second character, and stop as soon as the index is not greater than 2:| Current index | Included? (index > 2) | Character taken | …
- CBSE 2025Set 91/41 markMCQQ.Identify the correct output of the following code snippet: game="Olympic2024" print(game.index("C")) (A) 0 (B) 6 (C) -1 (D) ValueError
›Reveal solutionSolution
The
index()method searches for the first occurrence of a substring and returns its position (0-indexed). Since"C"does not exist in"Olympic2024", Python raises a ValueError.Why
index()behaves this wayThe
index()method in Python strings searches for the first occurrence of a substring and returns its zero-based position. The crucial detail: if the substring is not found,index()does not return-1or0— it raises aValueErrorexception and halts execution.This is different from the
find()method, which returns-1when the substring is absent. The designers of Python madeindex()strict: if you ask for the position of something that doesn't exist, that's an error condition worth stopping for.Step-by-step trace
-
Initialize the string
game = "Olympic2024"creates a string with characters at positions:Position 0 1 2 3 4 5 6 7 8 9 10 Character O l y m p i c 2 0 2 4 -
Search for
"C"The method
game.index("C")looks for the uppercase letter"C"in the string. -
Case-sensitive comparison
Python strings are case-sensitive. The string contains a lowercase
"c"at position 6, but"C"(uppercase) does not appear anywhere. -
Exception raised
Because
"C"is not found, Python raises:
-
- CBSE 2024Set 91/41 markMCQQ.Consider the statements given below and then choose the correct output from the given options: myStr="MISSISSIPPI" print(myStr[:4]+"#"+myStr[-5:]) (A) MISSI#SIPPI (B) MISS#SIPPI (C) MISS#IPPIS (D) MISSI#IPPIS
›Reveal solutionSolution
String slicing in Python extracts portions of a string using index notation;
myStr[:4]takes the first four characters, andmyStr[-5:]takes the last five, which are then concatenated with#in between.When you work with strings in Python, slicing is one of the most elegant operations at your disposal. It allows you to carve out any substring you need by specifying start and end positions. The syntax
[start:end]extracts characters from indexstartup to (but not including) indexend. When you omit the start, Python assumes you mean "from the beginning," and when you omit the end, it means "to the very end."Let's walk through the given code with the string
"MISSISSIPPI", which has 11 characters total (indices 0 through 10).The first slice is
myStr[:4]. Because there's no starting index, Python begins at position 0 and goes up to (but not including) index 4. That gives us:- Index 0: M
- Index 1: I
- Index 2: S
- Index 3: S
So
myStr[:4]produces"MISS".The second slice is
myStr[-5:]. Negative indices count backward from the end of the string. In"MISSISSIPPI", index -5 refers to the fifth character from the end. Let's map it out:- Index -5: S (the 7th character overall, at position 6)
- Index -4: I
- Index -3: P
- Index -2: P
- Index -1: I …
- CBSE 2024Set 91/41 markMCQQ.Q. 17 and 18 are ASSERTION (A) and REASONING (R) based questions. Mark the correct choice as (A) Both (A) and (R) are true and (R) is the correct explanation for (A) (B) Both (A) and (R) are true and (R) is not the correct explanation for (A) (C) (A) is true but (R) is false (D) (A) is false but (R) is true Assertion (A): The expression "HELLO".sort() in Python will give an error. Reason (R): sort() does not exist as a method/function for strings in Python.
›Reveal solutionSolution
The assertion is true — calling
.sort()on a string raises an error — and the reason correctly explains why: strings have nosort()method. Both are true, and (R) is the correct explanation for (A). The answer is (A).The concept: methods belong to types
In Python, every object has a specific type — integers, lists, strings, etc. Each type comes with its own set of methods (functions that belong to that type). For example, lists have
.append(),.pop(), and.sort(). Strings have.upper(),.lower(),.split(), and so on.The key question here is: does the string type have a
.sort()method? If not, calling it will produce anAttributeError— Python's way of saying "this object doesn't know how to do that."Step-by-step reasoning
-
What does
"HELLO".sort()actually try to do?The expression
"HELLO".sort()attempts to call a method namedsorton the string object"HELLO". Python will look for a method calledsortin the string class's definition. -
Does the string class have a
sortmethod?No. Strings in Python are immutable sequences of characters. They support methods like
.upper(),.lower(),.find(),.replace(), and.split(), but not.sort(). Thesort()method exists only on mutable sequence types like lists.Watch outA common mistake is to confuse strings with lists. Lists have
.sort()because they are mutable — you can rearrange their elements in place. Strings are immutable; you cannot change their characters in place, so sorting a string directly makes no sense. -
What happens when you run
"HELLO".sort()?Python raises an
AttributeErrorwith a message like:'str' object has no attribute 'sort'…
-
- CBSE 2023Set 91/11 markMCQQ.Select the correct output of the code: S="Amrit Mahotsav @ 75" A=S.partition(" ") print(A)(a) ('Amrit Mahotsav', '@', '75')(b) ['Amrit', 'Mahotsav', '@', '75'](c) ('Amrit', 'Mahotsav @ 75')(d) ('Amrit', ' ', 'Mahotsav @ 75')
›Reveal solutionSolution
The
partition()method splits a string into three parts at the first occurrence of the specified separator, returning a tuple. The output is ('Amrit', ' ', 'Mahotsav @ 75').The core concept here revolves around understanding how Python's string
partition()method works. Unlikesplit(), which can divide a string into many parts based on a separator and returns a list,partition()has a very specific job: it always divides the string into exactly three parts and returns them as a tuple.Here's the breakdown of what those three parts are:
- The part of the string before the first occurrence of the separator.
- The separator itself.
- The part of the string after the first occurrence of the separator.
The key is "first occurrence." If the separator appears multiple times,
partition()only considers the very first one it finds. If the separator is not found in the string,partition()returns a tuple containing the original string, followed by two empty strings('', '').Let's apply this to the given code.
-
Identify the string and the separator.
We have the string
S = "Amrit Mahotsav @ 75".The
partition()method is called withS.partition(" "), meaning the separator we are looking for is a single space character" ". -
Locate the first occurrence of the separator.
In the string "Amrit Mahotsav @ 75", the first space character appears immediately after the word "Amrit".
S = "Amrit Mahotsav @ 75"^(First space) -
Divide the string into three parts based on the first separator.
- Part 1 (before the separator): Everything before the first space is "Amrit".
- Part 2 (the separator itself): The separator is " ".
- Part 3 (after the separator): Everything after the first space is "Mahotsav @ 75".
-
Form the tuple.
The
partition()method returns these three parts as a tuple.So,
Awill be('Amrit', ' ', 'Mahotsav @ 75'). …
- CBSE 2023Set 91/41 markMCQQ.Select the correct output of the code : S="Amrit Mahotsav @ 75" A=S.split(" ",2) print(A)(a) ('Amrit', 'Mahotsav', '@', '75')(b) ['Amrit', 'Mahotsav', '@ 75'](c) ('Amrit', 'Mahotsav', '@ 75')(d) ['Amrit', 'Mahotsav', '@', '75']
›Reveal solutionSolution
The
split(" ",2)method splits the string into at most three parts using the space character as the delimiter, producing a list with the first two spaces as split points and the remainder as the third element.Let’s walk through what happens when Python runs this code. The string
S = "Amrit Mahotsav @ 75"contains three spaces — one after "Amrit", one after "Mahotsav", and one before "75". The methodsplit(" ",2)is called with two arguments: the delimiter (a single space) and the maximum number of splits (2).The key idea is that
splitwith amaxsplitargument does not split the string into all possible pieces. Instead, it performs exactly that many splits, starting from the left, and then lumps everything after the last split into one final piece. So withmaxsplit=2, Python will split at the first space and at the second space, but not at the third space. After the second split, whatever remains of the string — including any further spaces — becomes the third element of the resulting list.Let’s trace it:
- First split: at the space after "Amrit". We get
"Amrit"and the rest:"Mahotsav @ 75". - Second split: at the space after "Mahotsav". We get
"Mahotsav"and the remainder:"@ 75".
The remainder
"@ 75"is not split further because we have already used up both allowed splits. So the final result is a list with three elements:['Amrit', 'Mahotsav', '@ 75'].NoteThe
splitmethod always returns a list, not a tuple. Options (a) and (c) show tuples (with round brackets), so they are incorrect on that ground alone.Now look at the options:
- (a)
('Amrit', 'Mahotsav', '@', '75')— This is a tuple with four elements. Wrong on two counts: it’s a tuple, and it has four items. …
- First split: at the space after "Amrit". We get
- CBSE 2023Set 91/41 markMCQQ.Which of the following statement(s) would give an error after executing the following code ? S="Happy" # Statement 1 print(S*2) # Statement 2 S+="Independence" # Statement 3 S.append("Day") # Statement 4 print(S) # Statement 5(a) Statement 2(b) Statement 3(c) Statement 4(d) Statement 3 and 4
›Reveal solutionSolution
Statement 4 will cause an error because the
append()method is used for lists, not for string objects in Python.Let's break down each statement in the provided Python code to understand its function and identify any potential errors. Understanding how Python handles strings and their available methods is key here.
In Python, strings are immutable sequences of characters. This means once a string is created, its individual characters cannot be changed, nor can elements be added or removed directly using methods like
appendthat modify sequences in place.-
Statement 1:
S="Happy"This statement initializes a variable
Sand assigns it the string value "Happy". This is a perfectly valid assignment operation. -
Statement 2:
print(S*2)Here, the
*operator is used with a string (S) and an integer (2). In Python, when a string is multiplied by an integer, it results in the string being repeated that many times. So,S*2would evaluate to "HappyHappy". Theprint()function then displays this resulting string. This is a valid operation and will execute without error. -
Statement 3:
S+="Independence"This statement uses the augmented assignment operator
+=. For strings,+=performs string concatenation. It is equivalent toS = S + "Independence". The current value ofS("Happy") is concatenated with the string "Independence", and the new resulting string ("HappyIndependence") is then assigned back toS. This is a valid operation and will execute without error.NoteAfter Statement 3, the variable
Snow holds the value "HappyIndependence". -
Statement 4:
S.append("Day")…
-
🎓Unlock everything free for 14 days
- ✓Full step-by-step solutions
- ✓Concept-first explanations
- ✓Methods, shortcuts & mistakes
- ✓PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.