The Artima Developer Community
Sponsored Link

Python Buzz Forum
String Interpolation

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ian Bicking

Posts: 900
Nickname: ianb
Registered: Apr, 2003

Ian Bicking is a freelance programmer
String Interpolation Posted: Apr 21, 2005 1:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ian Bicking.
Original Post: String Interpolation
Feed Title: Ian Bicking
Feed URL: http://www.ianbicking.org/feeds/atom.xml
Feed Description: Thoughts on Python and Programming.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ian Bicking
Latest Posts From Ian Bicking

Advertisement

I was using string.Template in WSGIKit recently, and I started hacking silly things onto it with a dictionary that, for instance, turned d['str_v'] into repr(d['v']). But it got me to thinking about simple templates and string substitution, and the next thing you know I implemented something to do richer interpolation:

>>> name = 'Bob'
>>> e('Hi $name')
'Hi Bob'
>>> e('Hi ${name.lower()}')
'Hi bob'
>>> e('Your name is spelled: ${" ".join(list(name.upper()))}')
'Your name is spelled: B O B'

Maybe I should just use Cheetah and be done with it, but it was interesting to experiment. The source to e is, I think, relatively straight-forward. This has been done before in the guise of PEP 215. If I was to use this for real, I'd probably allow you to pass in a namespace as well -- it still adds value over plain Template by allowing expressions.

Read: String Interpolation

Topic: Where I live, on satellite (via Google Maps) Previous Topic   Next Topic Topic: Free gazebo plans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use