Python Pop Quiz - Letter Explosion
You learned about exploding a range object in the last quiz. For this quiz, you will extend that knowledge and discover what happens when you explode a string.
But wait! There’s more here than meets the eye. What is up with the numbers in the squiggly brackets?
If you’re familiar with string methods, you can figure this quiz out pretty quickly!
The Quiz
What will be output if you run this code?
A) ‘a, b, c’
B) ‘c, b, a’
C) ““
D) ‘b, a, c’
Hint
The Python documentation is your friend this time.
Stop here and try to figure out the quiz above. When you are ready, you can continue to the answer!
The Answer
B) ‘c, b, a’
Explanation
Python strings have many methods that you can utilize. In this case, you use the format() method. The squiggly numbers are a type of Format String Syntax.
In this case, you are telling Python that this string takes three parameters. You are also telling Python where those parameters should be inserted or interpolated into the string.
The string, "{2}, {1}, {0}", is saying that the third parameter is inserted first; the second parameter goes in the middle, and the first parameter is inserted last.
To get three parameters passed to this string, you use the single asterisk and unpack a letter-character string into a function.
You can use that function from the previous quiz here to prove what’s happening:
>>> def my_func(*args):
... print(args)
...
>>> my_func(*”abc”)
(’a’, ‘b’, ‘c’)Here you use the single asterisk to pass in three parameters: a, b and c.




Heh. "Exploding" a string. I like the terminology. 😆