String Formating¶
-
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<--
Use%d
as the specifier in the format string.
-
Given two integers
v1
andv2
, use string formatting to create a stringvalues
showing the values ofv1
andv2
with a ‘, ‘ between (a comma and a space).Make sure you put your values in a tuple.
-
If
v1
= 1,v2
= 3.75, andv3
= “hi”, create a string in variableresStr
with valuehi! 1 != 3.75 :-(
.Put your three values in a tuple in the orderv3
,v1
,v2
. To make sure you have only 2 digits after the decimal, use this format specifier:%.2f
-
If
v1
= 1, andv2
= 2, create a string in variableresStr
which refers tovariable 1 is 1 (1.0) and variable 2 is 2 (2.0)
.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.