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:4 | ISBN:978013274719 | Edition: 6

Question

Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following
statements:
(a) Check whether s1 is equal to s2 and assign the result to a Boolean variable
isEqual.
(b) Check whether s1 is equal to s2, ignoring case, and assign the result to a
Boolean variable isEqual.
(c) Check whether s1 has the prefix AAA and assign the result to a Boolean variable b.
(d) Check whether s1 has the suffix AAA and assign the result to a Boolean variable b.
(e) Assign the length of s1 to a variable x.
(f) Assign the first character of s1 to a variable x.
(g) Create a new string s3 that combines s1 with s2.
(h) Create a substring of s1 starting from index 1.
(i) Create a substring of s1 from index 1 to index 4.
(j) Create a new string s3 that converts s1 to lowercase.
(k) Create a new string s3 that converts s1 to uppercase.
(l) Create a new string s3 that trims whitespace characters on both ends of s1.
(m) Replace e with E in s1.
(n) Assign the index of the first occurrence of character e in s1 to a variable x.
(o) Assign the index of the last occurrence of string abc in s1 to a variable x.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Code:

s1 = " Welcome "
s2 = " welcome "

# (a)
# Check whether s1 is equal to s2 and assign the result to a Boolean variable
isEqual = s1 == s2
# Output: False
print(isEqual)  

# (b)
# Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual.
isEqual = s1.lower() == s2.lower()
# Output: True
print(isEqual)  

# (c)
# (c) Check whether s1 has the prefix AAA and assign the result to a Boolean variable b.
b = s1.startswith("AAA")
# Output: False
print(b)  

# (d)
# Check whether s1 has the suffix AAA and assign the result to a Boolean variable b.
b = s1.endswith("AAA")
# Output: False
print(b)  

# (e)
# Assign the length of s1 to a variable x.
x = len(s1)
# Output: 9
print(x)  

# (f)
# Assign the first character of s1 to a variable x.
x = s1[0]
# Output: ' '
print(x)  

# (g) 
# Create a new string s3 that combines s1 with s2.
s3 = s1 + s2
# Output: " Welcome  welcome "
print(s3)  

# (h)
# Create a substring of s1 starting from index 1.
sub1 = s1[1:]
# Output: "Welcome "
print(sub1)  

# (i)
# Create a substring of s1 from index 1 to index 4.
sub2 = s1[1:5]
# Output: "Welc"
print(sub2)  

# (j) 
# Create a new string s3 that converts s1 to lowercase.
s3 = s1.lower()
# Output: " welcome "
print(s3)  

# (k) 
# Create a new string s3 that converts s1 to uppercase.
s3 = s1.upper()
# Output: " WELCOME "
print(s3)  

# (l) 
# Create a new string s3 that trims whitespace characters on both ends of s1.
s3 = s1.strip()
# Output: "Welcome"
print(s3)  

# (m) 
# Replace e with E in s1.
s3 = s1.replace('e', 'E')
# Output: " WElcomE "
print(s3)  

# (n)
# Assign the index of the first occurrence of character e in s1 to a variable x.
x = s1.index('e')
# Output: 3
print(x)  

# (o) 
# Assign the index of the last occurrence of string abc in s1 to a variable x.
x = s1.rfind('abc')
# Output: -1
print(x)  

Output:

False
True
False
False
9
 
 Welcome  welcome 
Welcome 
Welc
 welcome 
 WELCOME 
Welcome
 WElcomE 
2
-1

 

1 0

Discussions

Post the discussion to improve the above solution.