0

I need to write a script to send an email using sendmail. The code that I'm looking at was found here: http://effbot.org/pyfaq/how-do-i-send-mail-from-a-python-script.htm

I am trying to figure out what this syntax means:

FROM = "sender@domain.ca"
TO = ["recipient@domain.ca"]

SUBJECT = "Hello!"

TEXT = "This message was sent via sendmail."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

From what I can gather the triple quote is used when you need to create a very long string with keywords so that python doesn't try to interpret them. However the percentage used after the triple quote is what intrigues me. Is it some sort of concatenate operator like ampersand?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
F. Gil
  • 11
  • 5
  • search for "python format string" : https://realpython.com/python-string-formatting/#1-old-style-string-formatting-operator , https://pyformat.info/ , etc. – Demi-Lune May 28 '20 at 15:21
  • Yes, the triple quote is for multi-line strings. It is irrelevant of keywords. The `%s` and corresponding values within the `%` afterward is what creates the text formatting. – patmcb May 28 '20 at 15:22

1 Answers1

-2

I'm almost certain this is a duplicate of something but if it's not, what you're looking at is a formatted string. Triple quote is a valid way to form a string in python.

see https://www.learnpython.org/en/String_Formatting

notacorn
  • 3,526
  • 4
  • 30
  • 60