String Formating

  1. Assume you have a variable x that refers to an integer. Write code to print out the value of x surrounded by arrows. E.g., if the value of x is 7, you should print this:

    -->7<--

     
    1
    x = 7
    2
    # replace this comment with your code
    3

    (strform1)

    Use %d as the specifier in the format string.
    2
     
    1
    print('-->%d<--' % x)
    2

    (strform1_a)

3
v1 = 73

(strform2_pre)

  1. Given two integers v1 and v2, use string formatting to create a string values showing the values of v1 and v2 with a ‘, ‘ between (a comma and a space).
    2
     
    1
    # replace this comment with your code
    2

    (strform2)

    Make sure you put your values in a tuple.
    2
     
    1
    values = '%d, %d' % (v1, v2)
    2

    (strform2_a)

4
v2 = 3.75

(strform3_pre)

  1. If v1 = 1, v2 = 3.75, and v3 = “hi”, create a string in variable resStr with value hi! 1 != 3.75 :-(.
    2
     
    1
    # replace this comment with your code
    2

    (strform3)

    Put your three values in a tuple in the order v3, v1, v2. To make sure you have only 2 digits after the decimal, use this format specifier: %.2f
    2
     
    1
    resStr = '%s! %d != %.2f :-(' % (v3, v1, v2)
    2

    (strform3_a)

3
v1 = 1

(strform4_pre)

  1. If v1 = 1, and v2 = 2, create a string in variable resStr which refers to variable 1 is 1 (1.0) and variable 2 is 2 (2.0).
    2
     
    1
    # replace this comment with your code
    2

    (strform4)

    To make sure you have only 1 digit after the decimal, use this format specifier: %.1f

    You can use a variable multiple times in the same tuple.

    2
     
    1
    resStr = 'variable 1 is %d (%.1f) and variable 2 is %d (%.1f)' % (v1, v1, v2, v2)
    2

    (strform4_a)