Skip to content

Instantly share code, notes, and snippets.

@ofelix03
Last active February 4, 2021 21:36
Show Gist options
  • Save ofelix03/556bc40b1bf3b7e17ac1101cd6fd07be to your computer and use it in GitHub Desktop.
Save ofelix03/556bc40b1bf3b7e17ac1101cd6fd07be to your computer and use it in GitHub Desktop.
% formatting string
first_name = "Samuel"
surname = "Mensah"
# Positional placeholders using %s
print("Hello %s %s" %(first_name, surname)) # Results: Hello Samuel Mensah
# Named positional placeholders using %(name)s
print("Hello %(first_name)s %(surname)s") # Result: Hello Samuel Mensah
first_name = "Samuel"
surname = "Mensah"
# Position placeholders using {}
print("Hello {} {}".format(first_name, surname) # Results: Hello Samule Mensah
# Keyword placeholders using {name}
print("Hello {first_name} {surname}".format(first_name=first_name, surname=surnam) # Results: Hello Samuel Mensah
# We can combine position placeholders with keyword placeholders
item_name = "LG UHD Monitor"
item_price = 30.20
item_currency_symbol = "$"
print("{} {} bought an {item_name} for {item_currency_symbol}{item_price}".format(first_name, surname, item_name=item_name, item_price=item_price, item_currency_symbol=item_currency_symbol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment