[Tutor] loops, variables and strings

James jtp at nc.rr.com
Sat Jul 5 16:57:40 CEST 2008


All,

I'm trying to do something pretty simple, but I can't seem to get
Python to behave nicely. :)

I'd like to automate a script that sends out three or four lists in an
e-mail. I have a function dedicated to sending e-mail that will take a
string variable, slap it in a message, and send it on its merry way.

The issue, however, is that I can't format the string I want
correctly. I'd like to do something similar to the following:

myString = """Hello. Below is an automated e-mail with important statistics.

The following user accounts expired today:
<user1> - <Full Name>
<user2> - <Full Name 2>
...

The following user accounts will expire within the next 7 days:
<user3> - <Full Name 3>
<user4> - <Full Name 4>
...

The following user accounts will expire in the next month:
<user 5> - <Full Name 5>
<user 6> - <Full Name 6>"""

I've written some code that actually does include a list of users, like this:

myString = """Hello. Below is an automated e-mail with important statistics.

The following user accounts expired today:
%s

The following user accounts will expire within the next 7 days:
%s

The following user accounts will expire in the next month:
%s""" % (expiredAccounts,toExpire7,toExpire30,),

Of course in this scenario, the variables in the parenthesis are
strings with newlines in them. But when I do a 'print myString', it
shows everything in one line, including the '\n' characters at the end
of every user account listed.

Thoughts on what I may be doing wrong?

Thanks!
- j


More information about the Tutor mailing list