like a "for loop" for a string

Vlastimil Brom vlastimil.brom at gmail.com
Sun Aug 17 16:05:52 EDT 2008


2008/8/17 Alexnb <alexnbryan at gmail.com>

>
> Basically I want the code to be able to pick out how many strings there are
> and then do something with each, or the number. When I say string I mean
> how
> many "strings" are in the string "string string string non-string string"
>
> Does that help?
>
> Not quite sure, if i underestand the task you want to achieve correctly,
but do you maybe mean something like the following? (here under "string" the
exact wording "string" is meant; obviously any section of the input string
will be a string too)

>>> fun_string = "string string string non-string non-string string"
>>> fun_words = fun_string.split()
>>> fun_words
['string', 'string', 'string', 'non-string', 'non-string', 'string']
>>> strings_from_fun_words = [word for word in fun_words if word ==
"string"]
>>> strings_from_fun_words
['string', 'string', 'string', 'string']
>>> len(fun_words)
6
>>> len(strings_from_fun_words)
4
>>>

HTH

  Vlasta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080817/dd125515/attachment-0001.html>


More information about the Python-list mailing list