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.
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