Strings and Printing

Indices and tables

Background

Getting to know the string class and the mini-format specification language is a great asset. These exercises will help with that.

Exercise

  1. Write “Hello World” using only Ascii literal character escapes.
  2. Use the string class to convert the following string to proper capitalization, “she wROTe a LoNG lETter bUt diDn’T ReAD it”.
  3. Write a function to wrap a user provided string, with the HTML tag provided and print it to the terminal.
  4. Write a function to insert a user provided string between all words in a string and print it to the terminal.
  5. Write a function to remove all new-lines in a string to create a single composite string and print it to the terminal.
  6. Write a function to add a prefix to all lines in a string.
  7. Write a function to draw a box using ascii characters, based on the user supplied width and height.
  8. Write a function to calculate the volume of a box, based on the user supplied width, height, depth (all given in the same units, which should be user supplied when the function is called). Print the result to the terminal to 3 decimal places and include the units and the cubed symbol.
  9. Print the following data to a table with the specifications given below:

Column 0

  • Width: 8
  • Heading: “Mode”
  • Value Display Format: 4 digit uppercase hex with 0x prefix, centered
  • Values: int 0 to 15,

Column 1

  • Width: 12
  • Heading: “Alpha”
  • Value Display Format: fixed point rounded to 2 decimal digits, with thousands separator, right justified
  • Values:
-5546.54725
22081.71617
-5965.92742
-2450.81368
28925.01114
18628.48674
23188.21829
5054.67254
6091.46452
-6351.53182
-2146.08381
-971.36459
5866.07492
30746.75865
-7623.53765
-10874.77380

Column 2

  • Width: 10
  • Heading: “Beta”
  • Value Display Format: Exponent notation, 3 digits of precision, with thousands separator, centered
  • Values:
25586.73057
10538.63947
-8944.90781
-7135.44111
28879.88609
4874.96768
29880.82047
1163.15909
20483.97234
-12741.09698
2610.09901
12375.65034
24253.62213
15498.02591
8220.57827
17235.47176

Hints

  • Ascii tables are ubiquitous on the internet, but, here is one source you can use.
  • When computing the volume in of the box in exercise #8, wrap the computation in the float() function to ensure it is not truncated. The use of floats and ints is covered in more detail when discussin numeric types.
  • When computing the volume of the box in exercise #8, keep in mind that the cubed symbol is NOT part of the Ascii character set. You will need to find its value in the Unicode character set and use the appropriate escape sequence to enter it.
  • For exercise #9, it would be easier to complete this if the loop, dict and tuple constructs were already introduced. However, they have not been, so, just use what you know to ‘brute force’ a solution.

Solution

When you are ready to see one possible solution, download and open this Jupyter Notebook file