Q.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
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 →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: …
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.