Question

In: Computer Science

3. Show the string that would result from each of the following string formatting operations. If...

3. Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why. Use Python shell to solve this question

(a) "Looks like {1} and {0} for breakfast".format("eggs", "spam") =>

(b) "There is {0} {1} {2} {3}".format(1,"spam", 4, "you") =>

(c) "Hello {0}".format("Susan", "Computewell") =>

(d) "{0:0.2f} {0:0.2f}".format(2.3, 2.3468) =>

(e) "{7.5f} {7.5f}".format(2.3, 2.3468) =>

(f) "Time left {0:02}:{1:05.2f}".format(1, 37.374) =>

(g) "{1:3}".format("14") =>

Solutions

Expert Solution

Question 3:
Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why. Use Python shell to solve this question

(a) "Looks like {1} and {0} for breakfast".format("eggs", "spam")
Ans: Legal

This is the correct way of using the format() method. The placeholders is defined using curly brackets{} and then we provide values for the following placeholders as parameters to that format() method.

(b) "There is {0} {1} {2} {3}".format(1,"spam", 4, "you") =>
Ans: Legal

This is also the correct way of using the format() method, we can use any data type as a parameter to the method format().

(c) "Hello {0}".format("Susan", "Computewell") =>
Ans: Legal

There can be any number of parameters for the method format(). It is necessary to use all those mentioned parameters.

(d) "{0:0.2f} {0:0.2f}".format(2.3, 2.3468) =>
Ans: Legal

We can even use the same placeholder twice. This is also valid.

(e) "{7.5f} {7.5f}".format(2.3, 2.3468) =>
Ans: Illegal

This is because the interpreter assumes the value inside the placeholder to an index. As we can see from the expression, we have used the index 7 for that, the number of arguments to the format() should be 8 accordingly. Since we don't have 8 values or 8 parameters in the format(). It raises an error stating the tuple index out of range.

(f) "Time left {0:02}:{1:05.2f}".format(1, 37.374) =>
Ans: Legal

Here, if we consider the statement. we notice that {0:02} indicates the first argument(0th index) with a field width of 2 and {1:05.2f} indicates the second argument(1st index/ with 2 decimal places of precision.

(g) "{1:3}".format("14") =>
Ans: Illegal

This is because, if we consider the statement {1:3} indicates a second argument with a field of width 3. Since there is no second argument. It raises an IndexError exception.

Please check the compiled program and its output for your reference which is done in Python shell:

(Feel free to drop me a comment, If you need any help)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

A. What effects would result from surgical removal of each of the following? (6 pts) •...
A. What effects would result from surgical removal of each of the following? (6 pts) • Stomach • Gall bladder • Pancreas B. Removal of which one would have the biggest impact on digestion? The smallest impact? Explain your reasoning.
1.) Indicate whether each of the following items would result in net cash flow from operating...
1.) Indicate whether each of the following items would result in net cash flow from operating activities being higher (H) or lower (L) than net income.             a.) Decrease in accounts payable.             b.) Depreciation expense.             c.) Decrease in inventory.             d.) Gain on sale of assets.             e.) Increase in accounts receivable.             f.) Increase in deferred tax liabilities.             g.) Decrease accrued liabilities.             h.) Increase in prepaid expenses.             i.) Increase in deferred revenue.             j.)...
(1) Show with the aid of diagrams the action of each of the following symmetry operations...
(1) Show with the aid of diagrams the action of each of the following symmetry operations on a point located at (x, y, z). C4 S4 {2 marks} (2) With reference to a set of character tables, determine the order of each of the following point groups. D2d Oh {2 marks} (3) Determine the total degeneracy of each of the following irreducible representations ().  = 3Ag + B1u + 2B2u  = A1g + 3Eg + 2T1g + T2u...
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
Select each of the following that would result in elevated cardiac output for a given heart...
Select each of the following that would result in elevated cardiac output for a given heart rate. In other words, which of the following would result in increased stroke volume? elevated end diastolic volume increased contractility physiologic cardiac hypertrophy pathologic cardiac hypertrophy reduced ejection fraction reduced preload increased blood volume increased hematocrit due to blood doping increased afterload increased circulating epinephrine
Show manual calculations to illustrate how the following operations would be performed in MATLAB. Determine the...
Show manual calculations to illustrate how the following operations would be performed in MATLAB. Determine the result and the size of the following expression. Note that the later arrays may depend on the definition of earlier arrays (a) a = 6:-3:-4 (b) b = [ a’ a’ a’] (c) c = b(1:2:3, 1:2:3) (d) d = b(3:-2:1,: ) (e) w = [zeros(1,3) , ones(3,1)’ , 3:5’]
Which of the following are phenotypes that would result from Tn5 transposition into luxI in Vibrio...
Which of the following are phenotypes that would result from Tn5 transposition into luxI in Vibrio harveyii? This strain will produce light when cells are given conditioned media from a wild type strain of Vibrio harveyii Inability of strain to produce light when cells are grown by themselves culture Conditioned media produced from this strain will complement light production of a luxI mutant
4. Which of the following responses would an economist expect to result from an increase in...
4. Which of the following responses would an economist expect to result from an increase in interest rates? (x) Thelma puts less money into savings accounts and bonds because a higher interest rate scares her since it is always an indication that the assets are more risky. (y) Since the interest expense on any given loan increases as the interest rate increases, Beatrice decides to purchase a smaller home than she had initially planned because her monthly income is fixed....
Digestion Essay Question (10 pts) A. What effects would result from surgical removal of each of...
Digestion Essay Question (10 pts) A. What effects would result from surgical removal of each of the following? (6 pts) • Stomach • Gall bladder • Pancreas B. Removal of which one would have the biggest impact on digestion?  The smallest impact? Explain your reasoning. (4 pts)
6. Which of the following would you expect to result from a block of electron transfer...
6. Which of the following would you expect to result from a block of electron transfer at complex II. Check all correct answers and explain. a. Reduction in oxidative phosphorylation b. No change in the proton gradient across the inner mitochondrial membrane c. FADH2 accumulation d. Damage to the cells due to reactive oxygen species accumulation Explanation:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT