string - Variable Substitution in Python -


so i'm working web.py , have following code:

check = db.select('querycode', where='id=$id', vars=locals())[0] 

which works fine, substitutes $id variable. in line not work:

web.sendmail('mail@mail.mail', "tomail@tomail.tomail", 'subject', 'hello $name') 

what wrong , how work. did concept right: $-sign substitutes variable?

python not in general php-style variable interpolation.

what seeing in first statement special feature of db.select picks variable values out of local variables in context of caller.

if want substitute in variable in second line, have manually 1 of ways python provides. here 1 such way.

web.sendmail('mail@mail.mail', "tomail@tomail.tomail", 'subject', 'hello %s' % name) 

here way.

web.sendmail('mail@mail.mail', "tomail@tomail.tomail", 'subject', 'hello {0}'.format(name)) 

the first option documented in string formatting operations.

see documentation str.format , format string syntax more details on second option.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -