age = 7 animal = 'turtle' weight = 317.5 # Format string to include animal, age, and weight to one digit after the decimal point. s = ("This %s is %i years old, and weighs %.1f pounds." % (animal, age, weight)) print(s)
|Description||Substitute variables in a string using the string format operator % as shown above. Symbols that can be used with % include: %i for integers, %f for floats and %s for strings.|
"%s" % "string" "%i" % 10 "%f" % 10.5
Updated on Mon Sep 21 15:53:25 2020.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License