[Tutor] string splitting (fwd)
Sun, 9 Dec 2001 20:49:19 -0800 (PST)
---------- Forwarded message ----------
Date: Sun, 9 Dec 2001 23:45:58 -0500
From: Kirk Bailey <email@example.com>
To: Danny Yoo <firstname.lastname@example.org>
Subject: Re: Re: [Tutor] string splitting
At 12/9/01 8:03:00 PM, you wrote:
>On Sun, 9 Dec 2001, Kirk Bailey wrote:
>> ok, in IDLE I created a variable, listnamemembers, and inserted a
>> bunch of info in it, with triple quotes.
>> listnamemembers = """email@example.com
>'listnamemembers' here is a string. Although we as humans might consider
>it a bunch of lines, Python's instincts is to treat strings as a sequence
>of single characters, which is why when we do a 'for' loop across it:
>> for i in listnamemembers:
>> print i
>Python will go through each character in 'listnamemembers'. That's why
>the program prints every character, one per line. Personally, I like
>this, because it allows certain things to be easy, like this:
Well, I read a file into the idle, and looked at the variable which I loaded it into with readlines, and it
was ['data\ndata\ndata\ndata\n'] which was disturbing. But educational.
>>>> def isVowel(letter):
>.... return letter in 'aeiouAEIOU'
>Anyway, back to your question: what we want to do, instead, is tell Python
>to "split" between the lines, to break our string down between new lines.
>We can do this if we use the string.split() function, like this:
>>>> string.split(listnamemembers, '\n')
>['firstname.lastname@example.org', 'email@example.com', 'Me@here2.com', 'firstname.lastname@example.org',
> 'email@example.com', '']
>That gives us a list of single lines, exactly what you're looking for!
>It's also something we can feed into a for loop:
>>>> for address in string.split(listnamemembers, '\n'):
>.... print "Here's one email address: ", address
>Here's one email address: firstname.lastname@example.org
>Here's one email address: email@example.com
>Here's one email address: Me@here2.com
>Here's one email address: firstname.lastname@example.org
>Here's one email address: email@example.com
>Here's one email address:
>In the command above, '\n' is the "newline" string, so we're telling
>Python to split up listnamemembers between new lines. We can be silly and
>split against something, else, like the '@' symbol:
>>>> string.split(listnamemembers, '@')
>['me', 'here.net\nyou', 'there.org\nMe', 'here2.com\neverybody',
> 'earth.edu\nnobody', 'domainerror.cc\n']
>Actually, this might actually be useful, since splitting between '@'
>allows us to pull out a user's name from their email address.
>string.split() is a very good tool, and worth using.
Well worth studying then. Grindgrindgrindgrind...
>> In total confusion,
>Hey, don't worry about it; it'll make sense eventually. Programming take
>practice, so don't be too hard on yourself. Just wondering, are you
>reading through a tutorial from the Python web site, or something else?
Yes, and yes. Book from library which is clear as mud, and 3 different tutorals, and the on line reference.
And this correspondance.
>I'm going to get some dinner now. I'll talk to you later!
Yum, Laser chicken stir fry. Quack-quack and I had ravioli earlier this evening.
Quack Quack, aka QUACKULA, is a quaker parrot. Got another one, Tiny, on my neck supervising right now. We
have a bunch of parrots ya see. Florida is funny that way, bird people all over, it's the climate. Where you
In total confusion,
Kirk D Bailey
| Consulting Agent Provecateur |
| Webmaster, Howlermonkey Email services Co. |
| Please visit us at http://www.howlermonkey.net/ |