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 Chapter 12: Strings. Students will explore fundamental string manipulation methods in Python, including capitalize(), center(), count(), decode(), encode(), endswith(), find(), and isalnum(). The solutions offer clear explanations and syntax for each method, along with practical examples and outputs to solidify understanding. These solutions are designed to help students grasp the nuances of working with strings, a crucial data type in programming. By working through these detailed explanations and examples, students can effectively prepare for their examinations, ensuring a strong foundation in Python string operations.

Quick info

BoardCBSE
ClassClass 11
SubjectComputer Science
Session2026
LanguageEnglish
TypeNCERT Solutions
ChapterChapter 12

Chapter summary

Chapter 12 of the CBSE Class 11 Computer Science syllabus covers the core concepts of Strings in Python. This NCERT Solutions set breaks down various string methods such as capitalize(), center(), count(), decode(), encode(), endswith(), find(), and isalnum(). It provides clear syntax, explanations, and outputs for each method, enabling students to understand how to manipulate and query strings effectively. This chapter is vital for building foundational programming skills in Python.

Learning outcomes

  • Understand the purpose and syntax of Python string methods.
  • Apply string methods like capitalize(), center(), and count() to manipulate strings.
  • Differentiate between encode() and decode() methods for string representation.
  • Utilize find() and endswith() to locate substrings within strings.
  • Determine if a string consists of alphanumeric characters using isalnum().
  • Interpret the output of Python code involving string methods.

Topics covered

Paper topics

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

Important topics

  • String Methods Overview
  • capitalize() and center()
  • count() and find() for substring searching
  • encode() and decode() for character encoding
  • isalnum() for character type checking
  • Understanding method parameters and return values

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 copy of the original string where only the first character is converted to uppercase, and all other characters are converted to lowercase. If the string does not start with a letter, or if it is empty, the original string is returned. This method does not modify the original string but returns a new string.

Question 2

Write the syntax for the capitalize() method.
Solution: The syntax for the `capitalize()` method in Python is as follows:
string.capitalize()
Here, `string` refers to the string object on which the method is called. This method does not require any arguments.

Question 3

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 `width`. The original string is placed in the center of this new string, and the remaining space on either side is filled with the specified `fillchar`. If `fillchar` is not provided, it defaults to a space character. The `width` parameter must be an integer.

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 width of the resulting centered string.
  2. fillchar: This is an optional parameter that specifies the character to be used for padding. If this parameter is omitted, the default padding character is a space (' ').

Question 5

Describe the count(str, beg=0, end=len(string)) method.
Solution: The `count(sub[, start[, end]])` method in Python returns the number of non-overlapping occurrences of a substring `sub` within the specified string. The optional `start` and `end` parameters define the slice of the string to search within. If `start` and `end` are not provided, the entire string is searched. The method returns an integer representing the count.

Question 6

Describe the decode(encoding='UTF8', errors='strict') method.
Solution: The `decode(encoding='UTF-8', errors='strict')` method in Python is used to decode a sequence of bytes (typically from an encoded string) into a regular string. It takes an `encoding` argument (defaulting to 'UTF-8') to specify the character encoding used, and an `errors` argument (defaulting to 'strict') to handle decoding errors. If `errors` is 'strict', an exception is raised for invalid bytes; other options include 'ignore' or 'replace'.

Question 7

What do you mean by encode(encoding='UTF-8', errors='strict')?
Solution: The `encode(encoding='UTF-8', errors='strict')` method in Python returns an encoded version of the string, converting it into a sequence of bytes. The `encoding` parameter specifies the encoding scheme to use (e.g., 'UTF-8', 'ASCII'), with 'UTF-8' being the default. The `errors` parameter determines how to handle encoding errors; 'strict' (the default) raises an exception, while 'ignore' skips problematic characters and 'replace' substitutes them.

Question 8

What do you mean by endswith(suffix, beg=0, end=len(string))?
Solution: The `endswith(suffix[, start[, end]])` method in Python checks if a string ends with a specified `suffix`. It returns `True` if the string ends with the given suffix, and `False` otherwise. The optional `start` and `end` parameters can be used to specify a slice of the string within which to perform the check.

Question 9

Write the syntax for the find() method.
Solution: The syntax for the `find()` method in Python is:
string.find(substring, start=0, end=len(string))
This method searches for the `substring` within the specified `string` (or a slice defined by `start` and `end`) and returns the lowest index where the substring is found. If the substring is not found, it returns -1.

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 output of the provided Python code will be:
15
15
-1
Explanation:
  • The first `print` statement searches for "exam" in `str1` from the beginning and finds it at index 15.
  • The second `print` statement searches for "exam" starting from index 10 and also finds it at index 15.
  • The third `print` statement searches for "exam" starting from index 40. Since "exam" does not occur at or after index 40, the method returns -1.

Question 11

Write the syntax for the 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 take 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 output of the provided Python code will be:
True
False
Explanation:
  • For the string "this2009", all characters are either letters or numbers, so `isalnum()` returns `True`.
  • For the string "this is string example....wow!!!", the presence of spaces and punctuation marks means not all characters are alphanumeric, so `isalnum()` returns `False`.

Common mistakes

  • Confusing the parameters and return values of string methods.
  • Incorrectly applying string methods to non-string data types.
  • Misinterpreting the output of find() when a substring is not present.
  • Errors in understanding the encoding and decoding processes.
  • Not considering the optional parameters (like beg and end) in methods like count() and find().

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., True/False, an integer, or a modified string).
  • Recreate the code examples provided in the solutions to reinforce understanding.
  • Focus on the difference between methods that modify strings (returning a new string) and those that return boolean values or indices.
  • Understand the role of optional parameters in methods like find() and count().

Practice MCQs

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

Q2. What is the primary function of the center() method in Python strings?

Q3. The count() method returns the number of occurrences of a substring. What are its optional parameters?

Q4. Which method is used to decode a string using a specified encoding?

Q5. What will be the output of `str.find(substring, 10)` if the substring is found starting at index 15?

Q6. The `isalnum()` method returns `True` if the string contains:

Frequently asked questions

What is the purpose of Chapter 12 in CBSE Class 11 Computer Science?

Chapter 12 focuses on Strings in Python, covering essential methods for manipulating and analyzing string data, which is fundamental for programming.

Which string methods are covered in these NCERT Solutions?

These solutions cover methods like capitalize(), center(), count(), decode(), encode(), endswith(), find(), and isalnum(), along with their syntax and usage.

How do these solutions help in exam preparation?

They provide clear, rewritten explanations, syntax details, and example outputs for each string method, aiding in a thorough understanding and revision for exams.

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

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

What does the find() method return if the substring is not found?

If the substring is not found within the specified range, the find() method returns -1.

Can I use these solutions for practice?

Yes, the solutions include code examples and outputs that are ideal for hands-on practice to reinforce learning.

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

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