Thursday, 26 December 2019

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

Python - String Manipulation (Part-1)

Question and Answers
CBSE Class 11 - Informatics Practices

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

Q1: What is a string in Python?

Answer: String is a sequence of characters, which is enclosed between either single(' ') quotes or double quotes(" "), python treats both single and double quotes same.
e.g. 
      str1 = "Welcome to the Python World."


Q2: Can a double-quoted string contain single quotes? If yes, give an example.

Answer: Yes. 
Example:
str1 = "Nitika's Fashions" 
print(str1)

Similarly, A single-quoted string can also contain double quotes:
Example:
str2 = '"Help!", he exclaimed.'
print(str2)

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



Q3: What is the difference between the following two statements?
print("Hello", "World")
print("Hello" + "World") 

Answer: The first statement displays the two words but one blank space between them. i.e. Hello World

The second statement concatenates the two strings (with any space between them) and displays it. i.e. HelloWorld

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


Q4: What are string comparison operators? How does string comparison work?

Answer: String comparison operators are: >,<,<=,<=,==,!=
>    Greater than
<    Less than
<= Less than and equal to
>= Greater than and equal to
== Equal to  
!=   not equal to
Python compares strings lexicographically i.e. using ASCII value of the characters.


Q5: What us the output of the following statements?
print("Nitika" == "Nikita")
print("Nitika" != "Nikita")
print("Nitika" > "Nikita")
print("Nitika" < "Nikita")
print("Nitika" >= "Nikita")
print("Nitika" <= "Nikita")
print("Nitika" > "")

Answer
print("Nitika" == "Nikita")  results False 
print("Nitika" != "Nikita")    results True
print("Nitika" > "Nikita")     results True
print("Nitika" < "Nikita")     results False
print("Nitika" >= "Nikita")  results True
print("Nitika" <= "Nikita")  results False
print("Nitika" > "")               results True

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


Q6: Are strings mutable or immutable? Explain with an example.

Answer: Python strings are immutable. This means that once they are assigned to a variable, their value cannot be changed.

Consider the following examples:
str1 = "eduvictors"
str2 = "eduvictors"

We use id( ) function to display memory address of str1 and str2 variables.
id(str1)
37702936
id(str2)
37702936

As both str1  and str2  points to the same memory location, hence they both point to the same object.

Now change value of str1,
            str1 +=  " solutions"
            id(str1)
            37914144
            id(str2)
            37702936

You can see now str1 points to totally different memory location, this shows that concatenation doesn't modify the original string object instead it creates a new string object.

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


Q7: What is the function of the following operators in string manipulation?
+, *, [ ], [ : ], in, not in, %
Give an example to display output using these operators.
Answer:
+  concatenates two strings 
*    replicate same string multiple times
[ ]  character of the string using indexing
[ : ] range slice
in membership check   
not in membership check for non-availability 
% formats the string 

Example code:
str1 = "eduvictors"
str2 = "solutions"

print("str1=", str1)
print("str2=", str2)
print("str1 + str2 = ", str1+str2)
print("str1 * 2 = ", str1*2)
print("str1[1]=", str1[1])
print("str1[3:9]=", str1[3:9])
print("character 'u' member of str1 =", 'u' in str1 )
print("%s welcomes you in Class %d"% ('Eduvictors', 11))

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


Q8: Does Python string support indexing?

Answer: Yes. Python strings can be indexed. It uses [ ] operator to get a character (as string type) at specified position. The first character in the sequence in the string is at the index 0.

e.g. 
   >>>a = "eduvictors"
   >>>print(a[1])
   'd'

Q9: Does Python string support negative indexing?

Answer:  Yes, it uses negative indexes to get the character from the end of the string
e.g.
   >>> a = "eduvictors"
   >>>print(a[-1])
   's'


Q10: If we try to get a character from an index that doesn't exist, what error will be shown by Python?

Answer: IndexError


Q11: Name the function used to find the length of a string.

Answer: len( )
e.g. 
   >>> len("eduvictors")
   10


Q12: What is the output of the following string?
    >>>"eduvictors"[3:]

Answer: 'victors' 


Q13: What is the output of the following string?
    >>>"eduvictors"[:3]

Answer: 'edu'


Q14: What are different string methods supported in Python?

Answer: To manipulate string, following methods can be used:

str.capitalize()  - To capitalize the string
str.upper() - Returns a copy of the string with all characters converted to uppercase
str.lower() - Returns a copy of the string with all characters converted to lowercase
str.str.find(sub) - To find the substring position
str.isalnum() - String consists of only alphanumeric characters (no symbols)
str.isalpha() - String consists of only alphabetic characters (no symbols)
str.islower() - String’s alphabetic characters are all lower case
str.isupper() - String’s alphabetic characters are all upper case
str.isnumeric() - String consists of only numeric characters
str.isspace() - String consists of only whitespace characters
str.istitle() - String is in title case
str.lstrip() - Returns a copy of the string with leading characters
str.rstrip() - Returns a copy of the string with leading characters
str.strip() - Returns a copy of the string with the leading and trailing characters removed.


Q15: What is the output of the following statements
>>>"eduvictors".upper()
>>>"welcome to python".capitalize()
>>>"PYTHON".lower()
>>>"python".islower()
>>>"python".isupper()
>>>" eduvictors  ".strip()
>>>"1234".isnumeric()
>>>"Hello".isnumeric()

Answer:
'EDUVICTORS'
'Welcome To Python'
'python'
True
False
'eduvictors'
True
False


Q16: Why there is an error in executing the following statements?
>>>str='honesty'
>>>str[2]='p'

Answer: Strings are immutable means that the contents of the string cannot be changed after it is created.


Q17: What is the output of the following statement?
>>> 3*"Hello"

Answer: 'HelloHelloHello'
The * operator repeats the string on the left hand side times the value on right hand side.


Q18: What is the syntax for  slicing a string?

Answer: String[<start>:<stop>:<step>]

The slice operator extracts sub parts from the strings from <start> position and ending at <end> position but not including character at <end> position.
Default step value for <step> is 1


Q19: What is the output of the following statements?
>>> "eduvictors"[1:5]
>>> "eduvictors"[1:5:2]

Answer: The first statement extracts characters from position 1 to position 5 but position 5 is not included. The second statement does the same except it extracts every second character from the selected range. 
Output is: 
'duvi'
'dv'


Q20: What is the output of "eduvictors"[::-1]?

Answer: 'srotcivude'
It reverses the string.

☞See also:

No comments:

Post a comment

We love to hear your thoughts about this post!