[Python-Dev] PEP 292, Simpler String Substitutions

Damien Morton damien.morton@acm.org
Wed, 19 Jun 2002 23:35:32 -0400


"I'd rather have a notation that's less error-prone than a better way
to check for errors.  (Not that PyChecker 2 isn't a great idea. :-)"

Percent notation "%s" notation already exists for strings.

Backquote notation is already in python, though I think, little used.

The $ notation reeks of obscure languages such as perl and shell.

Why not simply add backquote notation to python strings. I read in a
recent email from Timbot, I think,  that the backquote notation was
originally intended for string interpolation too.

name = "guido"
country = "the netherlands"
height = 1.92

"`name` is from `country`".sub() -> "guido is from the netherlands"
"`name.capitalize()` is from `country`" -> "Guido is from the
netherlands"

"`name` is %`height`4.1f meters tall".sub() -> "guido is 1.9 meters
tall"

"`name.capitalize()` can jump `height*1.7` meters".sub() -> "guido can
jump 3.264 meters"

You could probably also compile these interpolation strings as well.

Another thought:

One of the main problems with the "%(name)4.2f" notation is that the
format comes after the variable name. Is easy to forget adding the
actual format specifier in after the name.

Why not alter the notation to allow the format specifier to come before
the name part.

"%4.2f(height)" I think would be a whole lot less error prone, and would
allow for the format specifier to default to "s" where omitted.

"%(height)" is also less error prone, though it is ambiguous in the
current scheme.

Ive seen a nice class which evaluates the string used in the name part.

class itpl:
	def __getitem__(self, s):
		return eval(s, globals())