Skip to content

Instantly share code, notes, and snippets.

@hanvari
Last active June 9, 2020 05:55
Show Gist options
  • Save hanvari/5441e0b45df05f810ab927a4a93d3142 to your computer and use it in GitHub Desktop.
Save hanvari/5441e0b45df05f810ab927a4a93d3142 to your computer and use it in GitHub Desktop.
String Formatting & Substitution in Python

String Formatting & Substitution in Python

Method-1

data = {'unit': 'celsius', 'value':20}
"The temperature in %(unit)s is %(value)f degrees" % data

Method-2

data = {'unit': 'celsius', 'value':20}
"The temperature in {unit}s is {value}f degrees".format(**data)

Reference for further details:

https://pyformat.info/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment