Sal's Shipping (Walkthrough video)

Hello everyone! I’m trying to understand why at the end of the walk-through video of the Sal’s Shipping exercise the guy writes the following statement:

video: https://youtu.be/46_cL0O6xyQ

print(
“The cheapest option is $%.2f, via %s shipping method”
%(cost, method)
)

My question is why $%.2f and % calls for method and cost. Is the % symbol a call for variables? If so where can I find more applications of this symbol?

Thanks!

https://pyformat.info/

The % (per-cent) symbol traces back to an era when it meant a variable in the most literal sense. An unknown at time of writing. Be darned if I can find one good example, right now, but my peers will hopefully weigh in. From this is born, modulo formatting.

The symbol is twofold. In the string, a placeholder signal; and after the string, it acts as a parameter list signal. The modulo character is a signal.

Signal means indicator but it also means, heads up!

1 Like

You can also find it referred to as printf formatting, as it was implemented in C as part of the printf function. Don’t know whether printf was there from the start, but C has been around since '72. (Which is a lot longer than I!)

If you go back further, that method of formatting existed in BCPL (which influenced B, which in turn influenced C). Here’s an example of that in the writef function, in an implementation of the n-queens problem in BCPL.

GET "LIBHDR"

GLOBAL $(
	COUNT: 200
	ALL: 201
$)

LET TRY(LD, ROW, RD) BE
	TEST ROW = ALL THEN
		COUNT := COUNT + 1
	ELSE $(
		LET POSS = ALL & ~(LD | ROW | RD)
		UNTIL POSS = 0 DO $(
			LET P = POSS & -POSS
			POSS := POSS - P
			TRY(LD + P << 1, ROW + P, RD + P >> 1)
		$)
	$)

LET START() = VALOF $(
	ALL := 1
	FOR I = 1 TO 12 DO $(
		COUNT := 0
		TRY(0, 0, 0)
		WRITEF("%I2-QUEENS PROBLEM HAS %I5 SOLUTIONS*N", I, COUNT)
		ALL := 2 * ALL + 1
	$)
	RESULTIS 0
$)
2 Likes