Skip to content

Instantly share code, notes, and snippets.

@v
Created February 5, 2019 06:42
Show Gist options
  • Save v/ad15e5fd4e4e2f9c4c3ec64b721057b9 to your computer and use it in GitHub Desktop.
Save v/ad15e5fd4e4e2f9c4c3ec64b721057b9 to your computer and use it in GitHub Desktop.
Examples of string formatting
command = "SELECT * FROM whatever"
print(command)
fieldname = "age"
command = "SELECT " + fieldname + " from whatever"
print(command)
fieldname = 'age'
fieldname2 = 16
command = "SELECT %s from whatever WHERE %s=%d" % (fieldname, fieldname, fieldname2)
# SELECT age FROM whatever WHERE age=16
print(command)
command = "SELECT {fieldname} FROM whatever WHERE {fieldname}={fieldname2}".format(fieldname='name', fieldname2='Divya')
print(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment