SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
On Strings And Special Methods
Exercise:
Check Point
Question:1 | ISBN:978013274719 | Edition: 6

Question

Suppose that s1, s2, s3, and s4 are four strings, given as follows:
s1 = "Welcome to Python"
s2 = s1
s3 = "Welcome to Python"
s4 = "to"
What are the results of the following expressions?
a. s1 == s2
b. s2.count('o')
c. id(s1) == id(s2)
d. id(s1) == id(s3)
e. s1 <= s4
f. s2 >= s4
g. s1 != s4
h. s1.upper()
i. s1.find(s4)
j. s1[4]
k. s1[4 : 8]
l. 4 * s4
m. len(s1)
n. max(s1)
o. min(s1)
p. s1[-4]
q. s1.lower()
r. s1.rfind('o')
s. s1.startswith("o")
t. s1.endswith("o")
u. s1.isalpha()
v. s1 + s1

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

s1 = "Welcome to Python"
s2 = s1
s3 = "Welcome to Python"
s4 = "to"

a. s1 == s2                 # True
b. s2.count('o')            # 3
c. id(s1) == id(s2)         # True
d. id(s1) == id(s3)         # False
e. s1 <= s4                 # False
f. s2 >= s4                 # True
g. s1 != s4                 # True
h. s1.upper()               # "WELCOME TO PYTHON"
i. s1.find(s4)              # 8
j. s1[4]                    # "o"
k. s1[4:8]                  # "ome "
l. 4 * s4                   # "toto"
m. len(s1)                  # 17
n. max(s1)                  # "y"
o. min(s1)                  # " "
p. s1[-4]                   # "t"
q. s1.lower()               # "welcome to python"
r. s1.rfind('o')            # 14
s. s1.startswith("o")       # False
t. s1.endswith("o")         # True
u. s1.isalpha()             # False
v. s1 + s1                  # "Welcome to PythonWelcome to Python"

Explanation:

  • a. s1 == s2 compares the values of s1 and s2, which are the same, so it returns True.
  • b. s2.count('o') counts the occurrences of the letter 'o' in s2, which is 3.
  • c. id(s1) == id(s2) compares the memory addresses of s1 and s2, which are the same, so it returns True.
  • d. id(s1) == id(s3) compares the memory addresses of s1 and s3, which are different, so it returns False.
  • e. s1 <= s4 compares the lexicographical order of s1 and s4, 'Welcome to Python' is not less than 'to', so it returns False.
  • f. s2 >= s4 compares the lexicographical order of s2 and s4, 'Welcome to Python' is greater than 'to', so it returns True.
  • g. s1 != s4 compares the values of s1 and s4, which are not the same, so it returns True.
  • h. s1.upper() returns a new string with all characters in s1 converted to uppercase.
  • i. s1.find(s4) finds the index of the first occurrence of s4 in s1, which is at index 8.
  • j. s1[4] retrieves the character at index 4 in s1, which is 'o'.
  • k. s1[4:8] retrieves the substring from index 4 to index 7 (exclusive) in s1, which is 'ome '.
  • l. 4 * s4 repeats the string s4 four times, resulting in 'toto'.
  • m. len(s1) returns the length of s1, which is 17.
  • n. max(s1) returns the maximum character in s1 based on their Unicode values, which is 'y'.
  • o. min(s1) returns the minimum character in s1 based on their Unicode values, which is ' ' (space).
  • p. s1[-4] retrieves the character at index -4 in s1, which is 't'.
  • q. s1.lower() returns a new string with all characters in s1 converted to lowercase.
  • r. s1.rfind('o') finds the index of the last occurrence of 'o' in s1, which is at index 14.
  • s. s1.startswith("o") checks if s1 starts with the substring "o", which it does not, so it returns False.
  • t. s1.endswith("o") checks if s1 ends with the substring "o", which it does, so it returns True.
  • u. s1.isalpha() checks if all characters in s1 are alphabetic, but since s1 contains spaces and non-alphabetic characters, it returns False.
  • v. s1 + s1 concatenates s1 with itself, resulting in the string "Welcome to PythonWelcome to Python".

 

0 0

Discussions

Post the discussion to improve the above solution.