Strings Debugging#
Each of the following short code contains one or more bugs.
Please identify and correct these bugs.
Provide an explanation for your answer.
Question-1#
3.0*'HI'
Solution
Only integers can be used for repetition; 3.0 is a float.
Question-2#
3+'HI'
Solution
Concatenation can be done with two strings, but 3 is not a string. You can convert 3 to a string, and after that, you can perform concatenation: str(3) + 'HI'.
Question-3#
name = 'michael"
Solution
Both of the quotes on the left and right must be the same. Either 'michael'
or "michael"
will solve the problem.
Question-4#
print('he's coming')
Solution
The single quote of the string causes the problem since the string is also created by using single quotes.
To fix the problem you can either use
\
to make the single quote of the string a character:'he\'s coming'
Instead of single quotes, use double ones:
"he\'s coming"
Question-5#
name = 'Michael
Jordan'
print(name)
Solution
Triple single or double quotes must be used for strings that have multiple lines.
Question-6#
name = 'Brian'
print(name[5])
Solution
The index is out of range because the largest positive index is 4, as indexing starts from 0.
Question-7#
name = 'Brian'
print((12/3)*name)
Solution
The result of dividing 12 by 3 is 4.0, which is a float, so it cannot be used for repetition.
Question-8#
name = ''Brian''
Solution
Double single quotes cannot be used to create a string.