qa

John Machin sjmachin at lexicon.net
Mon Sep 17 18:07:02 EDT 2007


On Sep 18, 7:15 am, Shawn Minisall <trekker... at gmail.com> wrote:
> I'm trying to get a space in between these two strings but it's ignoring
> the space in between when it prints.
>
>  >>> string.capwords (string.join([s1 + " " + s2])) * 3
> 'Spam Ni!Spam Ni!Spam Ni!'
>  >>>

-1.  "qa" as a subject heading scarcely invites potential readers to
bother opening up your posting ...
0. Which 2 strings? You have got a space between 'spam' and 'ni'.
1. string.join([ONLY_ONE_element]) is a big fat no-operation.
2. It's ignoring what space when it prints?
3. Break your expression down into simple steps, so that you can see
where you are going wrong, like this:

>>> import string
>>> s1 = 'spam'
>>> s2 = 'ni!'
>>> s3 = s1 + " " + s2
>>> s3
'spam ni!'
>>> list1 = [s3]
>>> list1
['spam ni!']
>>> s4 = string.join(list1)
>>> s4
'spam ni!'
>>> s5 = string.capwords(s4)
>>> s5
'Spam Ni!'
>>> s6 = s5 * 3
>>> s6
'Spam Ni!Spam Ni!Spam Ni!'
>>>

Now: which part(s) of the above are not doing what you expected, and
what did you expect, and what is the basis for your expectation?

4. Almost all of the string module is deprecated -- instead you should
be using methods of string objects. Are you using an old book or
tutorial? If so, throw it away; it's well out of date.

5. capwords breaks the string into words (like the following) which is
a bit silly on a string that you've just laboriously constructed.

>>> def capwords(s):
...     return " ".join(w.capitalize() for w in s.split())
...
>>> capwords('foo bar zot')
'Foo Bar Zot'
>>>

6. What are you actually trying to achieve?





More information about the Python-list mailing list