Chapter 8
Strings and Functions In Python
Question Answers
Q1:- Define a string in Python.
Ans 1:- A string is a sequence of characters enclosed in either single quotes ('') or double quotes (“”). It is a
type of data type in Python.
For example- a= “hello".
Q2:- Name and explain the two membership operators.
Ans 2:- The two membership operators are as follows:
Operator Description Example
„a‟ in „hello‟
in Returns True if the substring is present in the False
given string, else returns False. „e‟ in „hello‟
True
„a‟ not in „hello‟
not in Returns True if the substring is not present in True
the string, else returns False. „e‟ not in „hello‟
False
Q3:- What is a slicing operator?
Ans 3:- String Slicing Operator is used to extract a part of the string from the main string. In this, the user is
required to specify the starting and ending index numbers, along with the step value.
Q4:- Write about index number and how is it assigned to a string?
Ans 4:- In Python, a string is a sequence of characters, and each character is assigned an index number starting
from 0. For example, in the string "hello", h is at index 0, e at index 1, l at index 2, and so on.
This indexing mechanism is essential for manipulating and accessing parts of strings in Python.
Q5:- List and explain any two functions in Python with examples.
Ans 5:- 1. len()- I returns the total number of characters in the string.
For eg- a= „hello‟
len(a)
Output= 5
2. lower()- It returns the string with all letters in lowercase.
For eg- a= „HELLO‟
a.lower()
Output= „hello‟
Q6:- Write the difference between slicing and indexing in Python.
Ans 6:- Indexing:
Accesses a single character in a string.
Syntax: string[index]
Example: hello[1] returns „e‟
Slicing:
Extracts a substring from a string.
Syntax: string[start:end]
Example: Hello[0:4] returns „Hell‟ (inclusive of start, exclusive of end).
Q7:- What are the different in- built functions performed on a string?
Ans 7:- In- built functions performed on a string are as follows:-
a. len()
b. title()
c. lower()
d. upper()
e. count()
f. replace(old, new)
g. isupper()
h. islower()
Q8:- Write the difference between upper() and lower() string functions.
Ans 8:- upper()- It returns the string with all letters in uppercase.
For example- a= „hello‟
a.upper()
Output- „HELLO‟.
lower()-It returns the string with all letters in lowercase.
For example- a= „Hello‟
a.lower()
Output- „hello‟
Q9:- Describe the function of replace (old, new).
Ans 9:- replace(old, new)- It returns a new string in which all occurrences of old strings are replaced with new
strings. For example: a= „hello‟
a.replace(„hello‟, „hello all‟)
Output- „hello all‟.
Q10:- What is the difference between concatenation and repetition.
Ans 10:- Concatenation- By using concatenation operator, user can combine two strings. Use the + (plus)
operator to join the strings.
For example-
a= „hello‟
b= „all‟
a+b
Output- „helloall‟.
Repetition- Repetition operator is used to repeat the given string for a specified number of times. Always use
“*” symbol to repeat the string as many times as specified by the user.
For example-
a= „Hello‟
a*3
Output- „HelloHelloHello‟