RE: Simple Problems: Caps and Commas

Discussion in 'Python' started by Robert Brewer, Oct 27, 2003.

  1. > >I tried this:
    > >-----------------------------------------------
    > >print "Name: ", fname, mname, lname
    > >print "Address: ",saddy, ",",city, ",",state, ",",zip
    > >-----------------------------------------------
    > >
    > >But I get extra spaces after each value:
    > >"Name: Firstname , Middlename , Lastname"
    > >"Address: Street Address , City , State , Zip"
    > >
    > >When it should look like this:
    > >"Name: Firstname, Middlename, Lastname"
    > >"Address: Street Address, City, State, Zip"

    >
    > That's because you put extra spaces in the string. Don't do
    > that, and it will
    > look right.


    I keep looking, but I don't see any "extra spaces". I think it's rather
    that the print command inserts spaces between arguments. If you don't
    like string interpolation, try:

    >>> print "Address: ", ", ".join((saddy, city, state, zip))




    Robert Brewer
    MIS
    Amor Ministries
    Robert Brewer, Oct 27, 2003
    #1
    1. Advertising

  2. Robert Brewer

    KefX Guest

    >I keep looking, but I don't see any "extra spaces".

    Then either you don't understand what I mean, or you're not looking hard
    enough.

    print "this, ", "that, ", "and the other"

    The "extra spaces" are the spaces after each comma in the first two
    double-quoted strings. It prints out:
    this, that, and the other

    instead of:
    this, that, and the other

    - Kef
    KefX, Oct 27, 2003
    #2
    1. Advertising

  3. On 27 Oct 2003 09:06:05 GMT, rumours say that OSPAM
    (KefX) might have written:

    >>I keep looking, but I don't see any "extra spaces".

    >
    >Then either you don't understand what I mean, or you're not looking hard
    >enough.


    Mu (which in this case it could mean, "The choices you offer aren't
    sufficient to cover what happens here, because you are overlooking
    something." :) Google for "jargon mu".

    >print "this, ", "that, ", "and the other"
    >
    >The "extra spaces" are the spaces after each comma in the first two
    >double-quoted strings. It prints out:
    >this, that, and the other
    >
    >instead of:
    >this, that, and the other


    KefX,

    the OP used the line:

    >print "Address: ",saddy, ",",city, ",",state, ",",zip


    This is what you are overlooking: the comma operator in the print
    argument list inserts spaces between arguments, therefore also between
    the variables and the literal commas (","). saddy, city and state don't
    end in a space, and there is no space in ",".

    The cryptic way to solve this is to fiddle with sys.stdout.softspace.
    The /preferably obvious/ way is to use string interpolation, which is
    definitely not overkill, if you do some benchmarking.

    Cheers :)
    --
    TZOTZIOY, I speak England very best,
    Ils sont fous ces Redmontains! --Harddix
    Christos TZOTZIOY Georgiou, Oct 27, 2003
    #3
  4. Robert Brewer

    KefX Guest

    >This is what you are overlooking: the comma operator in the print
    >argument list inserts spaces between arguments, therefore also between
    >the variables and the literal commas (","). saddy, city and state don't
    >end in a space, and there is no space in ",".


    Eep, I can't believe I didn't notice that. It's amazing what your eyes can do
    when you're not really paying attention.

    In that case, I'd just use the + operator instead of the comma operator to join
    the strings. Kind of lazy, but then, so am I ;)

    - Kef
    KefX, Oct 27, 2003
    #4
  5. On 27 Oct 2003 23:44:21 GMT, rumours say that OSPAM
    (KefX) might have written:

    >>This is what you are overlooking: the comma operator in the print
    >>argument list inserts spaces between arguments, therefore also between
    >>the variables and the literal commas (","). saddy, city and state don't
    >>end in a space, and there is no space in ",".

    >
    >Eep, I can't believe I didn't notice that. It's amazing what your eyes can do
    >when you're not really paying attention.


    That I can understand, being in an almost constant lack of time, so
    quite often I send a hasty reply which, when erroneous, doesn't help
    (and then pops up a clever youth like, ahem, Tim Peters to correct
    things). Just be more flexible (referring to your 'either - or' reply
    to Robert) if not sure :)

    >In that case, I'd just use the + operator instead of the comma operator to join
    >the strings. Kind of lazy, but then, so am I ;)


    String concatenation instead of interpolation? Now, *that's* overkill,
    performance-wise. Don't let the Martelligenzia di Ottimizzazione locate
    this post of yours, or you're in heap big trouble ;-)
    --
    TZOTZIOY, I speak England very best,
    Ils sont fous ces Redmontains! --Harddix
    Christos TZOTZIOY Georgiou, Oct 28, 2003
    #5
  6. Robert Brewer

    KefX Guest

    >String concatenation instead of interpolation? Now, *that's* overkill,
    >performance-wise. Don't let the Martelligenzia di Ottimizzazione locate
    >this post of yours, or you're in heap big trouble ;-)


    Eh, makes hardly a difference in code that isn't performance-critical, as this
    clearly wasn't. :) I prefer to just go with the obvious when performance is no
    issue. To me, string concatenation is more obvious than format strings. You can
    just glance at "one"+two and know that the value of 'two' will come right after
    the string 'one'. You don't have to look for the bit at the end showing what
    goes in the placeholders in the format string.

    - Kef
    KefX, Oct 28, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikael Petterson

    xsl syntax to generate all caps with _

    Mikael Petterson, Sep 10, 2003, in forum: XML
    Replies:
    3
    Views:
    905
    Dimitre Novatchev
    Sep 11, 2003
  2. Malcolm

    Caps convention.

    Malcolm, Dec 20, 2003, in forum: C Programming
    Replies:
    20
    Views:
    1,006
    Mark McIntyre
    Dec 21, 2003
  3. Kyle E
    Replies:
    6
    Views:
    259
    Kyle E
    Oct 28, 2003
  4. AviraM
    Replies:
    2
    Views:
    6,354
    Manish Pandit
    Sep 28, 2006
  5. darrel
    Replies:
    2
    Views:
    275
    darrel
    Dec 12, 2006
Loading...

Share This Page