Q: What is “Hello”.replace(“l”, “e”)?
Solution: Execute in shell to verify.
Q: To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed)?
Solution: __getitem(..) can be used to get character at index specified as parameter.
Q: To return the length of string s what command do we execute?
Solution: Execute in shell to verify.
Q: If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.
Solution: Execute in shell to verify.
Q: To check whether string s1 contains another string s2, use ________
Solution: s2 in s1 works in the same way as calling the special function __contains__ .
Q: Suppose i is 5 and j is 4, i + j is same as ________
Solution: Execute in shell to verify.
Q: What will be the output of the following Python code? class Count: def __init__(self, count = 0): self.__count = count c1 = Count(2) c2 = Count(2) print(id(c1) == id(c2), end = " ") s1 = "Good" s2 = "Good" print(id(s1) == id(s2))
Solution: Execute in the shell objects cannot have same id, however in the case of strings its different.
Q: What function do you use to read a string?
Solution: Execute in shell to verify.
Q: Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space).
Solution: Execute in the shell to verify.
You Have Score    | /9 |