0% found this document useful (0 votes)
3 views32 pages

7-Strings in Python Class 11 Notes

The document provides comprehensive notes on strings in Python for Class 11, covering definitions, operations, methods, and examples. Key topics include string immutability, accessing characters, string operations like concatenation and slicing, and various built-in functions for string manipulation. Additionally, it includes user-defined functions to demonstrate string handling techniques such as counting characters and reversing strings.

Uploaded by

shakupatel147
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

7-Strings in Python Class 11 Notes

The document provides comprehensive notes on strings in Python for Class 11, covering definitions, operations, methods, and examples. Key topics include string immutability, accessing characters, string operations like concatenation and slicing, and various built-in functions for string manipulation. Additionally, it includes user-defined functions to demonstrate string handling techniques such as counting characters and reversing strings.

Uploaded by

shakupatel147
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Strings in Python Class 11 Notes

Contents
1 Strings in Python Class 11 Notes
1.1 Strings
1.1.1 Accessing Characters in a String
1.1.2 String is Immutable
1.1.3 String Operations
[Link] Concatenation
[Link] Repetition
[Link] Membership
[Link].1 In
[Link].2 Not In
[Link] Slicing
1.1.4 Traversing a String
[Link] String Traversal Using for Loop:
[Link] String Traversal Using while Loop:
1.1.5 String Methods and Built-in Functions
[Link] len()
[Link] title()
[Link] lower()
[Link] upper()
[Link] count(str, start, end)
[Link] find(str, start, end)
[Link] index(str, start, end)
[Link] endswith()
[Link] startswith()
[Link] isalnum()
[Link] islower()
[Link] isupper()
[Link] isspace()
[Link] istitle()
[Link] lstrip()
[Link] rstrip()
[Link] strip()
[Link] replace(oldstr, newstr)
[Link] join()
[Link] partition()
[Link] split()
1.1.6 Handling Strings
[Link] Computer Science Class 11 Notes
[Link] Computer Science Class 11 MCQ
[Link] Computer Science Class 11 NCERT Solutions
Strings
A string is a group of one or more UNICODE characters in sequence. A letter, number,
space, or any other sign may be used as the character in this situation. One or more
characters can be enclosed in a single, double, or triple quote to produce a string.

Example –

>>> str1 = ‘Hello World!’


>>> str2 = “Hello World!”
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
>>> str3 = “””Hello World!”””
>>> str4 = ”’Hello World!”

Note – Python accepts single (‘), double (“), triple (”’) or triple(“””) quotes to denote string
literals. Single quoted strings and double quoted strings are equal. Triple quotes are used
for contain special characters like TAB, or NEWLINES.

Strings in Python Class 11 Notes

Accessing Characters in a String

A method known as indexing can be used to retrieve each individual character in a string.
The character to be retrieved in the string is specified by the index, which is enclosed in
square brackets ([ ]). The string has an index of 0 for the first character (counted from the
left) and n-1 for the last character, where n is the string’s length. We receive an IndexError
if we provide an index value outside of this range. A number must make up the index
(positive, zero or negative).

String is Immutable

A string is an immutable data type. It means that the contents of the string cannot be
changed after it has been created. An attempt to do this would lead to an error.

>>> str1 = “Hello World!”


#if we try to replace character ‘e’ with ‘a’
>>> str1[1] = ‘a’
TypeError: ‘str’ object does not support item assignment

Strings in Python Class 11 Notes

String Operations

A string is a group of letters and numbers. Concatenation, repetition, membership, and


slicing are just a few of the operations Python supports on the string data type. The

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


following subsections provide explanations of these procedures along with pertinent
examples.

Concatenation

To concatenate means to join. Python allows us to join two strings using concatenation
operator plus which is denoted by symbol +.

>>> str1 = ‘Hello’


>>> str2 = ‘World!’
>>> str1 + str2

Output:
Hello World!

Repetition

Python allows us to repeat the given string using repetition operator which is denoted by
symbol *.

>>> str1 = ‘Hello’


>>> str1 * 2

Output:
>>> str1 * 5
‘HelloHelloHelloHelloHello’

Membership

Python has two membership operators ‘in’ and ‘not in’. The ‘in’ operator takes two strings
and returns True if the first string appears as a substring in the second string, otherwise it
returns False.

In

>>> str1 = ‘Hello World!’


>>> ‘W’ in str1
True
>>> ‘Wor’ in str1
True
>>> ‘My’ in str1
False

Not In

>>> str1 = ‘Hello World!’


>>> ‘My’ not in str1
True
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
>>> ‘Hello’ not in str1
False

Slicing

In Python, to access some part of a string or substring, we use a method called slicing.
This can be done by specifying an index range. Given a string str1, the slice operation
str1[n:m].

>>> str1 = ‘Hello World!’


>>> str1[1:5]
‘ello’
>>> str1[7:10]
‘orl’
>>> str1[3:20]
‘lo World!’
>>> str1[7:2]

Strings in Python Class 11 Notes

Traversing a String

We can access each character of a string or traverse a string using for loop and while loop.

String Traversal Using for Loop:

>>> str1 = ‘Hello World!’


>>> for ch in str1:
print(ch,end = ”)
Hello World!

In the above code, the loop starts from the first character of the string str1 and
automatically ends when the last character is accessed.

String Traversal Using while Loop:

>>> str1 = ‘Hello World!’


>>> index = 0>>> while index < len(str1):
print(str1[index],end = ”)
index += 1
Hello World!

Here while loop runs till the condition index < len(str) is True, where index varies from 0 to
len(str1) -1.

Strings in Python Class 11 Notes

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


String Methods and Built-in Functions

There are numerous built-in functions in Python that let us operate with strings. a few of
the most popular built-in functions for manipulating strings.

len()

Returns the length of the given string

>>> str1 = ‘Hello World!’


>>> len(str1)
12

title()

Returns the string with first letter of every word in the string in uppercase and rest in
lowercase

>>> str1 = ‘hello WORLD!’


>>> [Link]()
‘Hello World!’

lower()

Returns the string with all uppercase letters converted to lowercase

>>> str1 = ‘hello WORLD!’


>>> [Link]()
‘hello world!’

upper()

Returns the string with all lowercase letters converted to uppercase

>>> str1 = ‘hello WORLD!’


>>> [Link]()
‘HELLO WORLD!’

count(str, start, end)

Returns number of times substring str occurs in the given string. If we do not give start
index and end index then searching starts from index 0 and ends at length of the string.

>>> str1 = ‘Hello World! Hello


Hello’
>>> [Link](‘Hello’,12,25)
2
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
>>> [Link](‘Hello’)
3

find(str, start, end)

Returns the first occurrence of index of substring str occurring in the given string. If we do
not give start and end then searching starts from index 0 and ends at length of the string. If
the substring is not present in the given string, then the function returns -1

>>> str1 = ‘Hello World! Hello Hello’


>>> [Link](‘Hello’,10,20)
13
>>> [Link](‘Hello’,15,25)
19
>>> [Link](‘Hello’)
0
>>> [Link](‘Hee’)
-1

index(str, start, end)

Same as find() but raises an exception if the substring is not present in the given string

>>> str1 = ‘Hello World! Hello


Hello’
>>> [Link](‘Hello’)
0
>>> [Link](‘Hee’)
ValueError: substring not found

Strings in Python Class 11 Notes

endswith()

Returns True if the given string ends with the supplied substring otherwise returns False

>>> str1 = ‘Hello World!’


>>> [Link](‘World!’)
True
>>> [Link](‘!’)
True
>>> [Link](‘lde’)
False

startswith()

Returns True if the given string starts with the supplied substring otherwise returns False

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


>>> str1 = ‘Hello World!’
>>> [Link](‘He’)
True
>>> [Link](‘Hee’)
False

isalnum()

Returns True if characters of the given string are either alphabets or numeric. If whitespace
or special symbols are part of the given string or the string is empty it returns False

>>> str1 = ‘HelloWorld’


>>> [Link]()
True
>>> str1 = ‘HelloWorld2’
>>> [Link]()
True
>>> str1 = ‘HelloWorld!!’
>>> [Link]()
False

islower()

Returns True if the string is non-empty and has all lowercase alphabets, or has at least
one character as lowercase alphabet and rest are non-alphabet characters

>>> str1 = ‘hello world!’


>>> [Link]()
True
>>> str1 = ‘hello 1234’
>>> [Link]()
True
>>> str1 = ‘hello ??’
>>> [Link]()
True
>>> str1 = ‘1234’
>>> [Link]()
False
>>> str1 = ‘Hello World!’
>>> [Link]()
False

isupper()

Returns True if the string is non-empty and has all uppercase alphabets, or has at least
one character as uppercase character and rest are non-alphabet characters

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


>>> str1 = ‘HELLO WORLD!’
>>> [Link]()
True
>>> str1 = ‘HELLO 1234’
>>> [Link]()
True
>>> str1 = ‘HELLO ??’
>>> [Link]()
True
>>> str1 = ‘1234’
>>> [Link]()
False
>>> str1 = ‘Hello World!’
>>> [Link]()
False

isspace()

Returns True if the string is non-empty and all characters are white spaces (blank, tab,
newline, carriage return)

>>> str1 = ‘ \n \t \r’


>>> [Link]()
True
>>> str1 = ‘Hello \n’
>>> [Link]()
False

istitle()

Returns True if the string is non-empty and title case, i.e., the first letter of every word in
the string in uppercase and rest in lowercase

>>> str1 = ‘Hello World!’


>>> [Link]()
True
>>> str1 = ‘hello World!’
>>> [Link]()
False

lstrip()

Returns the string after removing the spaces only on the left of the string

>>> str1 = ‘ Hello World!’


>>> [Link]()
‘Hello World!

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


Strings in Python Class 11 Notes

rstrip()

Returns the string after removing the spaces only on the right of the string

>>> str1 = ‘ Hello World!’


>>> [Link]()
‘ Hello World!’

strip()

Returns the string after removing the spaces both on the left and the right of the string

>>> str1 = ‘ Hello World!’


>>> [Link]()
‘Hello World!’

replace(oldstr, newstr)

Replaces all occurrences of old string with the new string

>>> str1 = ‘Hello World!’


>>> [Link](‘o’,’*’)
‘Hell* W*rld!’
>>> str1 = ‘Hello World!’
>>> [Link](‘World’,’Country’)
‘Hello Country!’
>>> str1 = ‘Hello World! Hello’
>>> [Link](‘Hello’,’Bye’)
‘Bye World! Bye’

join()

Returns a string in which the characters in the string have been joined by a separator

>>> str1 = (‘HelloWorld!’)


>>> str2 = ‘-‘ #separator
>>> [Link](str1)
‘H-e-l-l-o-W-o-r-l-d-!’

partition()

Partitions the given string at the first occurrence of the substring (separator) and returns
the string partitioned into three parts.
1. Substring before the separator
2. Separator
3. Substring after the separator
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
If the separator is not found in the string, it returns the whole string itself and two empty
strings

>>> str1 = ‘India is a Great Country’


>>> [Link](‘is’)
(‘India ‘, ‘is’, ‘ a GreatCountry’)
>>> [Link](‘are’)
(‘India is a Great Country’,’ ‘,”)

split()

Returns a list of words delimited by the specified substring. If no delimiter is given then
words are separated by space.

>>> str1 = ‘India is a Great Country’


>>> [Link]()
[‘India’,’is’,’a’,’Great’, ‘Country’]
>>> str1 = ‘India is a Great Country’
>>> [Link](‘a’)
[‘Indi’, ‘ is ‘, ‘ Gre’, ‘t Country’]

Strings in Python Class 11 Notes

Handling Strings

In this section, we’ll discover how to use user-defined functions in Python to manipulate
strings in various ways.

Q. Write a program with a user defined function to count the number of times a
character (passed as argument) occurs in the given string.

#Program
#Function to count the number of times a character occurs in a
def charCount(ch,st):
count = 0
for character in st:
if character == ch:
count += 1
return count
st = input(“Enter a string: “)
ch = input(“Enter the character to be searched: “)
count = charCount(ch,st)
print(“Number of times character”,ch,”occurs in the string is:”,count)

Output:
Enter a string: Today is a Holiday
Enter the character to be searched: a
Number of times character a occurs in the string is: 3
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
Strings in Python Class 11 Notes

Q. Write a program with a user defined function with string as a parameter which
replaces all vowels in the string with ‘*’.

#Program
#Function to replace all vowels in the string with ‘*’
def replaceVowel(st):
newstr = ”
for character in st:
if character in ‘aeiouAEIOU’:
newstr += ‘*’
else:
newstr += character
return newstr
st = input(“Enter a String: “)
st1 = replaceVowel(st)
print(“The original String is:”,st)
print(“The modified String is:”,st1)

Output:
Enter a String: Hello World
The original String is: Hello World
The modified String is: H*ll* W*rld

Q. Write a program to input a string from the user and print it in the reverse order
without creating a new string.

#Program
#Program to display string in reverse order
st = input(“Enter a string: “)
for i in range(-1,-len(st)-1,-1):
print(st[i],end=”)

Output:
Enter a string: Hello World
dlroW olleH

Q. Write a program which reverses a string passed as parameter and stores the
reversed string in a new string. Use a user defined function for reversing the string.

#Program
#Function to reverse a string
def reverseString(st):
newstr = ” #create a new string
length = len(st)
for i in range(-1,-length-1,-1):
newstr += st[i]
return newstr
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
#end of function
st = input(“Enter a String: “)
st1 = reverseString(st)
print(“The original String is:”,st)
print(“The reversed String is:”,st1)

Output:
Enter a String: Hello World
The original String is: Hello World
The reversed String is: dlroW olleH

Strings in Python Class 11 Notes

Q. Write a program using a user defined function to check if a string is a palindrome


or not. (A string is called palindrome if it reads same backwards as forward. For
example, Kanak is a palindrome.)

#Program
#Function to check if a string is palindrome or not
def checkPalin(st):
i=0
j = len(st) – 1
while(i <= j):
if(st[i] != st[j]):
return False
i += 1
j -= 1
return True
#end of function
st = input(“Enter a String: “)
result = checkPalin(st)
if result == True:
print(“The given string”,st,”is a palindrome”)
else:
print(“The given string”,st,”is not a palindrome”)

Output 1:
Enter a String: kanak
The given string kanak is a palindrome
Output 2:
Enter a String: computer
The given string computer is not a palindrome

String in Python Class 11 MCQ


1. _________ is a sequence which is made up of one or more UNICODE characters.
a. String
b. Number

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


c. Float
d. Double

a. String
2. String can be a _________.
a. Letter
b. Digit
c. Whitespace & Symbol
d. All of the above

d. All of the above


3. A string can be assign by enclosing ______ quote.
a. Single
b. Double
c. Triple
d. All of the above

d. All of the above


4. Each individual character in a string can be accessed using a technique called
_______.
a. Indexing
b. Method
c. Storing
d. None of the above

a. Indexing
String in Python Class 11 MCQ

5. The index specifies the character to be accessed in the string and is written in
square brackets _______.
a. ( )
b. [ ]
c. { }
d. None of the above

b. [ ]
6. In the string, the index of the first character start from ______.
a. 0
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
b. n-1
c. 1
d. None of the above

a. 0
7. In the string, the index of the last character ______.
a. 0
b. n-1
c. 1
d. None of the above

b. n-1
8. If we give index value out of this range then we get an _______.
a. Run time Error
b. IndexError
c. Syntax Error
d. None of the above

b. IndexError
9. What will be the output of the following python code
str1=”Welcome to my \n School”
print(str1)
a. Welcome to my
b. Welcome to my School
c. Welcome to my
School
d. None of the above

c. Welcome to my
String in Python Class 11 MCQ

10. What will be the output of the following python code


str1=”Welcome to \
my School”
print(str1)
a. Welcome to my School
b. Welcome to
my School
c. Welcome to \ my School
d. None of the above

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


a. Welcome to my School
11. A string is an _______ data type.
a. Mutable
b. Immutable t
c. Both a) and b)
d. None of the above

b. Immutable
12. Immutable data type means _________.
a. String can be changed after it has been created
b. String cannot be changed after it has been created
c. Both a) and b)
d. None of the above

b. String cannot be changed after it has been created


13. Concatenate means ______.
a. to Join
b. to Divide
c. to Split
d. None of the above

a. to Join
14. Python allows us to join two strings using concatenation operator _______.
a. + +
b. + –
c. +
d. None of the above

c. +
String in Python Class 11 MCQ

15. Python allows us to repeat the given string using repetition operator which is
denoted by symbol _________.
a. *
b. &
c. #
d. $

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


a. *
16. It is possible to retrieve each individual character in a string using a method
_________ .
a. Concatenation
b. Indexing
c. Replication
d. All of the above

b. Indexing
17. We receive a ________ if we provide an index value that is outside of the range.
a. Syntax Error
b. Run time Error
c. Index Error
d. None of the above

c. Index Error
18. Python has two membership operators _______ and ______.
a. In and Out
b. In and Not
c. True and False
d. None of the above

b. In and Not
19. The _________ operator also takes two strings and returns True if the first string
does not appear as a substring in the second string, otherwise returns False.
a. Not
b. Not to
c. Not in
d. Not out

c. Not in
String in Python Class 11 MCQ

20. In Python, to access some part of a string or substring, we use a method called
_______.
a. Joining
b. Slicing

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


c. Accessing
d. None of the above

b. Slicing
21. The string’s initial character is at index 0 from ________.
a. Right side
b. Left side
c. Both a) and b)
d. None of the above

b. Left side
22. If a string’s contents cannot be modified, the string is __________.
a. Immutable
b. Mutable
c. Both a) and b)
d. None of the above

a. Immutable
23. String data types can perform _______ operations in Python.
a. Slicing
b. Concatenation
c. Membership
d. All of the above

d. All of the above


24. We can access each character of a string or traverse a string using ______ and
________.
a. If and for loop
b. for and while loop
c. for and do-while loop
d. None of the above

b. for and while loop


String in Python Class 11 MCQ

25. ________ returns the length of the given string.


a. title()
b. len()
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
c. length()
d. None of the above

b. len()
26. _______ returns a list of words delimited by the specified substring. If no
delimiter is given then words are separated by space.
a. partition ()
b. split()
c. divide()
d. None of the above

b. split()
27. Following is an example of _________.
str1 = “[Link]”
a. String
b. Dictionary
c. List
d. None of the above

a. String
28. What will be the output of the following code.
str1=”Welcome”
str1=”Welcome to my School”
print(str1)
a. Welcome
b. Welcome to my School
c. Welcome Welcome to my School
d. None of the above

b. Welcome to my School
29. _______returns the string with first letter of every word in the string in uppercase
and rest in lowercase.
a. upper()
b. lower()
c. title()
d. None of the above

c. title()

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


String in Python Class 11 MCQ

30. ________ returns the string with all uppercase letters converted to lowercase.
a. lower()
b. upper()
c. title()
d. None of the above

a. lower()
31. ________returns the string with all lowercase letters converted to uppercase.
a. lower()
b. upper()
c. title()
d. None of the above

b. upper()
32. Same as find() but raises an exception if the substring is not present in the given
string.
a. find(str, start, end)
b. index(str, start, end)
c. endswith()
d. None of the above

b. index(str, start, end)


33. _______ returns True if the given string ends with the supplied substring
otherwise returns False.
a. find(str, start, end)
b. index(str, start, end)
c. endswith()
d. None of the above

c. endswith()
34. What will be the output of the following python code
str1=”WelcometomySchool”
print(len(str1))
a. 17
b. 18
c. 19
d. 20

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


a. 17
String in Python Class 11 MCQ

35. What will be the output of the following python code


str1=”’Welcome
to
my
School”’
print(str1)
a. Welcome to my School
b. Welcome
to
my
School
c. Welcome to
my School
d. None of the above

b. Welcome
to
my
School
36. ________ returns True if characters of the given string are either alphabets or
numeric. If whitespace or special symbols are part of the given string or the string is
empty it returns False.
a. isalnum()
b. islower()
c. isupper()
d. None of the above

a. isalnum()
37. What will be the output of the following python code
str1=”Welcome to my School”
print(len(str1))
a. 17
b. 18
c. 19
d. 20

d. 20

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


38. _______ returns True if the string is non-empty and has all lowercase alphabets,
or has at least one
character as lowercase alphabet and rest are non-alphabet characters.
a. isalnum()
b. islower()
c. isupper()
d. None of the above

b. islower()
39. ________ returns True if the string is non-empty and all characters are white
spaces (blank, tab,
newline, carriage return).
a. isspace()
b. istitle()
c. istrip()
d. rstrip()

a. isspace()
String in Python Class 11 MCQ

40. ________ returns the string after removing the spaces both on the left and the
right of the string.
a. isspace()
b. strip()
c. istrip()
d. rstrip()

b. strip()
41. ________ returns the string after removing the spaces only on the left of the
string.
a. isspace()
b. istitle()
c. istrip()
d. rstrip()

c. istrip()
42. ________ returns the string after removing the spaces only on the right of the
string.
a. isspace()
b. istitle()

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


c. istrip()
d. rstrip()

d. rstrip()
43. What will be the index value of ‘h’ in the following python code
str1=”Python”
a. 3
b. 4
c. 5
d. 6

a. 3
44. What will be the output of the following python code
str1=”Python”
str1[0]=”M”
print(str1)
a. Python
b. Mython
c. Pythonm
d. None of the above

b. Mython
String in Python Class 11 MCQ

45. What will be the output of the following python code


str1=”Python”
print(str[5])
a. Index Error
b. Run time Error
c. Output Error
d. None of the above

a. Index Error
46. What will be the output of the following python code
print(“Python”.replace(“Python”,”My Python”))
a. Python My Python
b. Python
c. My Python
d. None of the above

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


c. My Python
47. What will be the output of the following python code
for i in “Python”:
print(i)
a. Python
b. Py
th
on
c. P
y
t
h
o
n
d. None of the above

c. P
y
t
h
o
n
48. Which operator can be applied to both integer and string values?
a. *
b. /
c. %
d. –

a. *
49. What will be the output of the following python code
str1 = 20 + “20”
print(str1)
a. Syntax Error
b. TypeError
c. No Error
d. None of the above

b. TypeError
String in Python Class 11 MCQ

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


50. Which operator is used to concatenate strings?
a. ++
b. +
c. –
d. /

b. +
51. What will be the output of the following python code
print(len(“\\\@\\\/@”))
a. 6
b. 7
c. 8
d. 9

b. 7
52. What will be the output of the following python code
for i in (1,2,3,4):
print(“*” * i)
a.
*
**
***
****
b.
*
****
c.
****
***
**
*
d. None of the above

a.
*
**
***
****
53. What will be the output of the following python code
print(“Python”[2:3])
a. Error
b. h

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


c. t
d. No Output

c. t
54. What will be the output of the following python code
print(“P-y-t-h-o-n”.split(“-“,3))
a. [‘P’, ‘y’, ‘t’, ‘h-o-n’]
b. [‘P’, ‘y’, ‘t-h-o-n’]
c. [‘P’, ‘y-t-h-o-n’]
d. None of the above

a. [‘P’, ‘y’, ‘t’, ‘h-o-n’]


String in Python Class 11 MCQ

55. What will be the output of the following python code


print(“Welcome to my School”.find(“my”,4))
a. 09
b. 10
c. 11
d. 12

c. 11
56. Which of the following is known as mapping data type?
a. List
b. Dictionary
c. Tuple
d. String

b. Dictionary
57. What will be the output of the following python code.
str1 = “Welcome to my School”
[Link](“School”)
a. Value Error
b. No Output
c. Syntax Error
d. None of the above

b. No Output

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


58. The third parameter of a slice operation can also be used to specify the
_________.
a. Step Size
b. Indexing
c. Both a) and b)
d. None of the above

a. Step Size
59. What will be the output of the following python code.
print(“Welcome to my School”.count(“m”,0))
a. 0
b. 1
c. 2
d. 3

c. 2
String in Python Class 11 MCQ

60. The reverse string will be returned by which of the following?


a. str[::1]
b. str[::-1]
c. Both a) and b)
d. None of the above

b. str[::-1]
61. What will be the output of the following python code.
str1 = “Python”
print([Link](‘h’))
a. 3
b. 5
c. 2
d. 6

a. 3
62. What will be the output of the following python code.
str1=”Python”
print([Link]())
a. Error
b. True
c. False
d. None of the above
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
c. False
63. What will be the output of the following python code.
str1=”PYTHON”
print([Link]())
a. PYTHON
b. pYTHON
c. Python
d. None of the above

c. Python
64. What will be the output of the following python code.
str1=”Python Python Python Python”
print([Link](“Python”))
a. 3
b. 4
c. 5
d. 6

b. 4
String in Python Class 11 MCQ

65. What will be the output of the following python code.


print(“$”.join(“Python”))
a. p$y$t$h$o$n
b. P$y$t$h$o$n
c. $p$y$t$h$o$n
d. $P$y$t$h$o$n

b. P$y$t$h$o$n
66. Which of the python function return Boolean value?
a. index()
b. find()
c. endwith()
d. None of the above

c. endwith()
67. The word(s) separated by the specified substring are returned by the split()
function.

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


a. Tuple
b. List
c. Dictionary
d. None of the above

b. List
68. When using the split() method, words are separated by ______ if a delimiter is
not provided.
a. Semi colon
b. Comma
c. Colon
d. Space

d. Space

String Manipulation in Python Class 11 Questions and Answers


1. Consider the following string mySubject:
mySubject = “Computer Science”
What will be the output of the following string operations :
i. print(mySubject[0:len(mySubject)])
Answer – Computer Science

ii. print(mySubject[-7:-1])
Answer – Scienc

iii. print(mySubject[::2])
Answer – Cmue cec

iv. print(mySubject[len(mySubject)-1])
Answer – e

v. print(2*mySubject)
Answer – Computer ScienceComputer Science

vi. print(mySubject[::-2])
Answer – eniSrtpo

vii. print(mySubject[:3] + mySubject[3:])


Answer – Computer Science

viii. print([Link]())
Answer – cOMPUTER sCIENCE

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


ix. print([Link](‘Comp’))
Answer – True

x. print([Link]())
Answer – False

2. Consider the following string myAddress:


myAddress = “WZ-1,New Ganga Nagar,New Delhi”

What will be the output of following string operations :

i. print([Link]())
Answer – wz-1,new ganga nagar,new delhi

ii. print([Link]())
Answer – WZ-1,NEW GANGA NAGAR,NEW DELHI

iii. print([Link](‘New’))
Answer – 2

iv. print([Link](‘New’))
Answer – 5

v. print([Link](‘New’))
Answer – 21

vi. print([Link](‘,’))
Answer – [‘WZ-1’, ‘New Ganga Nagar’, ‘New Delhi’]

vii. print([Link](‘ ‘))


Answer – [‘WZ-1,New’, ‘Ganga’, ‘Nagar,New’, ‘Delhi’]

viii. print([Link](‘New’,’Old’))
Answer – WZ-1,Old Ganga Nagar,Old Delhi

ix. print([Link](‘,’))
Answer – (‘WZ-1’, ‘,’, ‘New Ganga Nagar,New Delhi’)

x. print([Link](‘Agra’))
Answer – ValueError: substring not found

String Manipulation in Python Class 11 Solutions


3. Write a program to input line(s) of text from the user until enter is pressed. Count
the total number of characters in the text (including white spaces),total number of
alphabets, total number of digits, total number of special symbols and total number
of words in the given text. (Assume that each word is separated by one space).
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
Answer –
str = input(“Enter your text : “)
Alpha = 0
Digit = 0
Special = 0
words = 0
for ch in str:
if [Link]():
Alpha += 1
elif [Link]():
Digit += 1
else:
Special += 1

for ch1 in str:


if [Link]():
words += 1
print(“Alphabets: “,Alpha)
print(“Digits: “,Digit)
print(“Special Characters: “,Special)
print(“Words in the Input :”,(words + 1))

Output
Enter your text : Hello how are you
Alphabets: 14
Digits: 0
Special Characters: 3
Words in the Input : 4

4. Write a user defined function to convert a string with more than one word into title
case string where string is passed as parameter. (Title case means that the first
letter of each word is capitalised)
Answer –
def convertToTitle(string):
titleString = [Link]();
print(titleString)

str = input(“Type your Text : “)


Space = 0
for b in str:
if [Link]():
Space += 1

if([Link]()):
print(“The String is already in title case”)
elif(Space > 0):
convertToTitle(str)
else:
print(“One word String”)
Chap-7 DR AMISHA DALAL COMPUTER SCIENCE
Output
Type your Text : Welcome to my school
Welcome To My School

5. Write a function deleteChar() which takes two parameters one is a string and
other is a character. The function should create a new string after deleting all
occurrences of the character from the string and return the new string.
Answer –
def deleteChar(string,char):
str = “”
for ch in string:
if ch != char:
str+=ch
return str

string = input(“Enter a string: “)


char = (input(“Enter a character: “))[0]
print(deleteChar(string,char))

6. Input a string having some digits. Write a function to return the sum of digits
present in this string.
Answer –
def myfunction(string):
sum = 0
for a in string:
if([Link]()):
sum += int(a)
return sum

str = input(“Enter any string with digits : “)


result = myfunction(str)
print(“The sum of digits in the string : “,result)

Output
Enter any string with digits : Welcome 225
The sum of digits in the string : 9

7. Write a function that takes a sentence as an input parameter where each word in
the sentence is separated by a space. The function should replace each blank with a
hyphen and then return the modified sentence.
Answer –
def myfunction():
string = input(“Enter a sentence: “)
str = [Link](” “,”-“)
print(str)

myfunction()

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE


Output
Enter a sentence: Welcome to my school
Welcome-to-my-school

Chap-7 DR AMISHA DALAL COMPUTER SCIENCE

You might also like