Quiz

A string is assigned to a variable
a = 'Amazing'
Which expressions will let you print the second character 'm' of this String

a[2] a[1] a[0] a[-6] Index numbers start with 0.

A string is assigned to a variable
a = 'Amazing'
Which expression will get you the characters from position 0 to index of 'z', including 'z'

a[0:a.find("z")+1] a[0:a.find("z")-1] a[0:3] a[:4] a[0:4]

A string is assigned to a variable
a = 'Amazing'
You wrote an expression to find if the given string has a substring 'zing'
a.find('zing')
This expression returns the -

index number 3 where it found the substring zing index number 4 where it found the substring zing returns the substring zing None of the above

A string is assigned to a variable
a = 'amazing'
You wrote an expression to capitalize this string
a.capitalize()
print(a)
This will print -

amazing Amazing None of the above This method returns a new string with the first letter in uppercase. The String 'a' is not changed

A string is assigned to a variable
a = 'amazing'
You wrote an expression to find its length
print(len(a))
This will print -

7 6 1

What is the result of evaluating this expression
'amazing'.count('a', 1)

1 2 0 Since the start position 1 is given, it counts the number of 'a' from index position 1

What is the result of evaluating this expression
'amazing'.count('a', 0, 2)

1 2 0 Since the start position 0 and the end position 2 is given, it counts the number of 'a' from index position 1 to index position 2, excluding 2

Study the code below
pi = 3.141592653589793
b = '{:.3f}'.format(pi)
print(b)
What is the output of the print statement?

3.142 0.142 003.14 Output is restricted to 3 decimal points due to using '.3f'. Digits before the decimal point are untouched.

Study the code below
pi = 3.141592653589793
b = '{:.3f}'.format(pi)
print(type(b))
What is the data type of 'b'?

str int bool Format function converts a numeric value to a String

Study the code below
t1 = (5, 8)
t2 = (10,15)
d = 'X: {0[0]}; Y: {1[1]}'
d.format(t1, t2)
What is the output?

'X: 5; Y: 15' 'X: 5; Y: 8' 'X: 5; Y: 10' Follow the index positions of the arguments passed in to find the correct output

What is output of the given expression?
a=3.14159
{:.0f}.format(a)

Error '314159' '314159.0' Missing quotes for the formatter expression. So the error

What is output of the given expression?
'{1:}{0}'.format('B', 10)

10B 10:B B10

Exercise

results matching ""

    No results matching ""