[Tutor] loops, variables and strings

bob gailer bgailer at gmail.com
Sat Jul 5 20:21:20 CEST 2008


James wrote:
> 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,),
>
>   
Remove the trailing , in the last line. It is causing myString to be a 
tuple.

> 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?


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list