CBSE Class 11 Computer Science Chapter 12: Strings NCERT Solutions

NCERT Solutions PDF Class 11 PDF

This chapter provides essential NCERT Solutions for Class 11 Computer Science, focusing on 'Strings'. It covers fundamental string manipulation methods in Python, including capitalize(), center(), count(), decode(), encode(), endswith(), find(), and isalnum(). The solutions explain the purpose, syntax, and return values of each method with clear examples. Understanding these string operations is crucial for data processing and text manipulation in programming. These solutions are designed to help students grasp the concepts thoroughly and prepare effectively for their board examinations by offering detailed explanations and practical insights into string handling in Python.

Quick info

BoardCBSE
ClassClass 11
SubjectComputer Science
Session2026
LanguageEnglish
TypeNCERT Solutions
Chapter12. Strings

Chapter summary

Chapter 12, 'Strings', in the CBSE Class 11 Computer Science curriculum focuses on the fundamental concepts and methods of string manipulation in Python. The NCERT Solutions provided here detail various built-in string functions such as capitalize(), center(), count(), decode(), encode(), endswith(), find(), and isalnum(). Each solution clarifies the functionality, parameters, and output of these methods, aiding students in understanding how to effectively work with text data.

Learning outcomes

  • Understand the purpose and functionality of various Python string methods.
  • Learn the syntax and parameters for methods like capitalize(), center(), count(), find(), and isalnum().
  • Determine the return values for string methods based on their input.
  • Apply string methods to manipulate and analyze text data.
  • Interpret the output of Python code involving string operations.

Topics covered

Paper topics

  • String Methods
  • capitalize()
  • center()
  • count()
  • decode()
  • encode()
  • endswith()
  • find()
  • isalnum()
  • String Manipulation
  • Python Strings
  • Text Processing

Important topics

  • String Methods Overview
  • capitalize() and center()
  • count() and find()
  • encode() and decode()
  • isalnum() and endswith()

PDF preview

Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1

Explain the capitalize() method in Python.
Solution: The capitalize() method in Python is used to create a modified copy of a string. This method ensures that the very first character of the string is converted to its uppercase equivalent, while all subsequent characters in the string are converted to their lowercase equivalents. If the string begins with a non-alphabetic character, it remains unchanged, and the method proceeds to lowercase the rest of the string.

Question 2

Write the syntax for the capitalize() method.
Solution: The syntax for the capitalize() method in Python is straightforward. It is called on a string object and does not require any arguments:

string.capitalize()

Here, 'string' represents the variable holding the string you wish to capitalize.

Question 3

Following is the syntax for capitalize() method: str.capitalize(). What value will be returned by the center(width, fillchar) method in Python?
Solution: The center(width, fillchar) method in Python returns a new string of a specified total width. This new string is centered relative to the original string's content. The padding on either side of the original string is achieved using the specified 'fillchar'. If 'fillchar' is not provided, it defaults to a space character. The returned string will have a length equal to the 'width' parameter.

Question 4

What are the two parameters of the center() method?
Solution: The center() method in Python accepts two parameters:
  1. width: This is a mandatory integer parameter that specifies the total length of the resulting centered string.
  2. fillchar: This is an optional parameter. It specifies the character to be used for padding the string on both sides to reach the desired width. If this parameter is not provided, the default padding character is a space (' ').

Question 5

Describe the count(str, beg=0, end=len(string)).
Solution: The count() method in Python is used to determine the number of non-overlapping occurrences of a specified substring within a given string. It can optionally take 'beg' (start index) and 'end' (end index) parameters to define a specific slice of the string within which to perform the count. The default behavior, if 'beg' and 'end' are not specified, is to count occurrences throughout the entire string. The method returns an integer representing the total count.

Question 6

Describe the decode(encoding='UTF8', errors='strict').
Solution: The decode() method in Python is used to convert a sequence of bytes (typically obtained from an encoded string) back into a regular string. It requires an 'encoding' parameter, which specifies the codec used for decoding (e.g., 'UTF-8', 'ascii'). The 'errors' parameter dictates how to handle decoding errors; 'strict' (the default) raises an exception, while other options like 'ignore' or 'replace' can be used to manage problematic byte sequences.

Question 7

What do you mean by encode(encoding='UTF-8', errors='strict')?
Solution: The encode() method in Python is used to convert a string into a sequence of bytes. It takes an 'encoding' parameter, specifying the desired encoding format (like 'UTF-8', 'ascii', etc.). The 'errors' parameter determines how to handle characters that cannot be encoded; 'strict' (the default) will raise an error, while 'ignore' will skip such characters, and 'replace' will substitute them with a placeholder.

Question 8

What do you mean by endswith(suffix, beg=0, end=len(string))?
Solution: The endswith() method in Python checks if a string ends with a specified suffix. It returns True if the string concludes with the given 'suffix', and False otherwise. Optional 'beg' and 'end' parameters can be provided to specify a slice of the string within which to perform the check. If these are omitted, the entire string is considered.

Question 9

Write the syntax for the find() method.
Solution: The syntax for the find() method in Python is as follows:

string.find(substring, start, end)

Here, 'string' is the string being searched, 'substring' is the sequence to find, 'start' is the optional starting index for the search, and 'end' is the optional ending index for the search.

Question 10

Write the output of the following code. #!/usr/bin/python str1 = "this is string example... ,wow!!!"; str2 = "exam"; print str1.find(str2); print str1.find(str2,10); print str1.find(str2, 40);
Solution: The provided Python code demonstrates the usage of the find() method with different starting positions.
  1. print str1.find(str2): This searches for the substring "exam" in "this is string example... ,wow!!!" starting from the beginning (index 0). The substring "exam" is found starting at index 15.
  2. print str1.find(str2, 10): This searches for "exam" starting from index 10. It is found again at index 15.
  3. print str1.find(str2, 40): This searches for "exam" starting from index 40. Since the string is shorter than index 40 and "exam" does not appear from index 40 onwards, the substring is not found. In this case, find() returns -1.
Therefore, the output of the code will be:

15 15 -1

Question 11

Write the syntax for isalnum() method.
Solution: The syntax for the isalnum() method in Python is:

string.isalnum()

This method is called on a string object and does not require any arguments. It returns True if all characters in the string are alphanumeric (either letters or numbers) and there is at least one character, otherwise it returns False.

Question 12

Write the output of the following code. #!/usr/bin/python str = "this2009"; # No space in this string print str.isalnum(); str = "this is string example....wow!!!"; print str.isalnum();
Solution: The code tests the isalnum() method on two different strings.
  1. str = "this2009"; print str.isalnum();: The string "this2009" contains only alphabetic characters ('t', 'h', 'i', 's') and numeric digits ('2', '0', '0', '9'). Since all characters are alphanumeric and the string is not empty, the isalnum() method returns True.
  2. str = "this is string example....wow!!!"; print str.isalnum();: This string contains spaces (' ') and punctuation marks ('.', '!'). These characters are not alphanumeric. Therefore, the isalnum() method returns False.
The output of the code will be:

True False

Common mistakes

  • Confusing the parameters and return types of different string methods.
  • Incorrectly applying string methods to non-string data types.
  • Misinterpreting the output of methods like find() when the substring is not present.
  • Errors in understanding the default values for optional parameters in string methods.

Revision tips

  • Practice using each string method with different inputs to observe their behavior.
  • Pay close attention to the return values of each method (e.g., boolean, integer, new string).
  • Review the syntax and parameter definitions for each method to avoid common errors.
  • Work through the provided code examples and try to predict the output before checking the solution.

Practice MCQs

Q1. Which string method returns a copy of the string with only its first character capitalized?

Q2. What does the center() method return when used with a string?

Q3. How many times does the substring 'is' occur in 'this is a test string'?

Q4. Which method is used to encode a string into a specific format?

Q5. What is the return value of str.find(sub) if the substring 'sub' is not found in 'str'?

Q6. The isalnum() method returns True if all characters in the string are:

Frequently asked questions

What is the main purpose of the Strings chapter in Class 11 Computer Science?

This chapter introduces students to the fundamental concepts of strings in Python and various built-in methods used for manipulating and processing text data effectively.

Which string method is used to check if a string ends with a specific sequence of characters?

The endswith() method is used to check if a string terminates with a specified suffix. It returns True if it does, and False otherwise.

What is the difference between encode() and decode() methods?

The encode() method converts a string into a sequence of bytes using a specified encoding, while the decode() method converts a sequence of bytes back into a string using a specified encoding.

How does the find() method work in Python strings?

The find() method searches for a specified substring within a string and returns the lowest index in the string where the substring is found. If the substring is not found, it returns -1.

Can the center() method be used without a fill character?

Yes, if the fillchar parameter is omitted, the center() method defaults to using a space character for padding.

What kind of output can be expected from the isalnum() method?

The isalnum() method returns a boolean value: True if all characters in the string are alphanumeric (letters or numbers) and the string is not empty, and False otherwise.

Content reviewed by the NCERT Help team. Editorial Team and update policy

NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.