Pages

Wednesday, 1 January 2020

CBSE Class 11 - Informatics Practices - Python - String Manipulation (Part-2) - Question and Answers(#class11Python)(#eduvictors)

Python - String Manipulation (Part-2)

Question and Answers
CBSE Class 11 - Informatics Practices

CBSE Class 11 - Informatics Practices - Python - String Manipulation (Part-2) - Question and Answers(#class11Python)(#eduvictors)


Q1: What are membership operators for strings?

Answer: Two membership operators are:
in Returns True if a substring exists in the given string otherwise returns false.
             e.g. "xy" in "xyz"  #returns True
                    "ab" in "xyz"  #returns False

not in Returns True is a substring does not exist in the given string;
e.g. "ab" not in "xyz"   #returns True
       "xy"  not in "xyz"   #returns False


Q2: What is the output of the following?
str1 = "Hello World!"
str2 = "he"
str3 = "He"
str2 in str1
str3 in str1



Answer:
False
True
str2 in str1 returns False since "he" substring is not part of string "Hello World!"
str3 in str1 returns True since "He" substring is part of string "Hello World!"


Q3: What does the following string functions do?
(a) endswith( )
(b) startswith( )
(c) count( )
(d) find( )
(e) rfind( )



Answer:
(a) endswith( s1: str): bool
Returns True if string str ends with substring s1 otherwise returns False
e.g. "Welcome".endswith("me")    #returns True

(b) startswith(s1: str): bool
Returns True if string str starts with substring s1 otherwise returns False
e.g. "Welcome".startswith("We")  #returns True

(c) count(substring): int
Returns number of occurrences of substring the string.
e.g. "Anybody is somebody".count("body") #returns 2

(d) find(s1): int
Returns lowest index from where s1 starts in the string, if string not found returns -1
e.g.  "Welcome".find("o") #returns 4 

(e) rfind(s1): int
Returns highest index i.e. last occurence from where s1 starts in the string, if string not found returns -1
e.g. "Welcome To India".rfind("o") #returns 9



Q4: What does find( ) do? Write its syntax.

Answer: The syntax is: find(sub[,start[, end]])
The function is used to search the first occurrence of the substring in the given string. It returns the index at which the substring starts. It returns -1 if the substring does occur in the string.

start - optional parameter, its default value is 0. It points to postion from where the search starts.

end - optional parameter, its default value is end of the string. It points to position where to end the search.


Q5: What is the output of the following statements?
>>> str = "mammals"
>>> str.find("ma")

Answer: 0


Q6: What is the output of the following statements?
>>> str = "mammals"
>>> str.find("ma", 2)

Answer: 3


Q7: What is the output of the following statements?
>>> str = "mammals"
>>> str.find("ma", 2, 4)


Answer: -1


Q8: What does replace( ) function does? Give an example.

Answer: replace(old, new) - The function replaces all the occurrences of the old string with the new string
e.g.
>>> str1 = 'Hello'
>>> print(str1.replace('l', 'L'))
HeLLo



Q9: What is the output of the following?
>>> str1 ="WelcoMe"
>>> str1.swapcase()

Answer: wELCOmE

swapcase( ) function returns the string with case changes.


Q10: What is the use of count( ) method? Give an example.

Answer: The count() method returns the number of times a specified value appears in the string.
Its syntax is:
string.count(value, start, end)
where,
value is the string to search for,
start - an optional parameter, the position to start the search. Default is 0
end  - an optional parameter, The position to end the search. Default is the end of the string.

e.g.
>>> str1 = "To be or not to be that is the question"
>>> str1.count("be")
2


☞See Also:
Python - String Manipulation (Part-1) Q & A

Buy Python Textbook: 
    ☞ Informatics Practices with Python: Textbook for CBSE Class 11
    ☞ Oswaal CBSE Question Bank Class 11 Informatics Practice Book Chapterwise & Topicwise Includes Objective Types & MCQ's (For March 2020 Exam)

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.