Harshveer Singh
Harshveer Singh

Reputation: 4197

calling function in print(""" """)

I want to call a function in formatted output of string using print(""" """) function of Python. For example:

print("""
something.......something...
abs(-10.5)
and then again some string......
""")

Is there any way to do it?

Upvotes: 3

Views: 290

Answers (3)

Don Question
Don Question

Reputation: 11614

print "whatever you want %s and then some" %abs(x)

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336388

This is how you can do it using the new and improved string formatting method (Python 2.6 and up):

print("""
something.......something...
{0}
and then again some string......
""".format(abs(-10.5)))

Upvotes: 14

Rob Wouters
Rob Wouters

Reputation: 16327

You should do it like this:

print("""
something.......something...
%s
and then again some string......
""" % abs(-10.5))

Instead of %s you can for example use %.3f which will give you 3 decimals precision, i.e. 10.500. See the docs for more information.

Upvotes: 4

Related Questions