3.2 Exercise with strings
1.Initialize the string “abc” on a variable named “s”:
(a) Use a function to get the length of the string.
(b) Write the necessary sequence of operations to transform the string “abc” in
“aaabbbccc”. Suggestion: Use string concatenation and string indexes.s='abc'
print(len(s))
print('{0}{0}{0}{1}{1}{1}{2}{2}{2}'.format('a','b','c'))
print(s[0]*3+s[1]*3+s[2]*3)3
aaabbbccc
aaabbbccc
2.Initialize the string “aaabbbccc” on a variable named “s”:
(a) Use a function that allows you to find the first occurence of “b” in the string,
and the first occurence of “ccc”.
(b) Use a function that allows you to replace all occurences of “a” to “X”, and
then use the same function to change only the first occurence of “a” to “X”.s='aaabbbccc'
print(s.find('b'))
print(s.find('ccc'))
print(s.find('a'))
print(s.replace(s[4],'X'))
print(s.replace('a','X',1))3
6
0
aaaXXXccc
Xaabbbccc
3.Starting from the string “aaa bbb ccc”, what sequences of operations do you need
to arrive at the following strings? You can find the “replace” function.
(a) “AAA BBB CCC”
(b) “AAA bbb CCC”s='aaa bbb ccc'
s=s.upper()
print(s)
print(s.replace('B','b'))AAA BBB CCC
AAA bbb CCC