misguided
misguided

Reputation: 3789

How do I format a string within a function call?

I want to pass a string to a variable to a string, which in turn gets passwd onto a function. I had written the code below:

Not Working

env = 'D'
myFunction("Checking Processes A%s/B%s",'')% (env,env)

error

myFunction("Checking Processes A%s/B%s",'')% (env,env)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

Since the code failed I re-wrote it as below:

Working

env = 'D'
str = "Checking Processes A%s/B%s" %(env,env)
myFunction(str,'')

Can anyone suggest any alternative shorter approaches to this ?Ideally in lines of my first attemp which would mean less LOC.

Upvotes: 0

Views: 126

Answers (2)

dawg
dawg

Reputation: 103774

Could do this:

>>> "Checking Processes A{}/B{}".format(env,env)
'Checking Processes AD/BD'

Upvotes: 1

zhangyangyu
zhangyangyu

Reputation: 8610

myFunction("Checking Processes A%s/B%s" % (env,env),'')

Upvotes: 0

Related Questions