Puzzled

Discussion in 'Python' started by Colin J. Williams, Jul 12, 2005.

  1. The snippet of code below gives the result which follows

    for k in ut.keys():
    name= k.split('_')
    print '\n1', name
    if len(name) > 1:
    name[0]= name[0] + name[1].capitalize()
    print '2', name
    name[0]= name[0].capitalize()
    print '3', name

    1 ['logical', 'or']
    2 ['logicalOr', 'or']
    3 ['Logicalor', 'or']

    I was expecting that 3 would read ['LogicalOr', 'or']

    If I replace the above code with:

    for k in ut.keys():
    name= k.split('_')
    print '\n1', name
    if len(name) > 1:
    name[0]= name[0].capitalize() + name[1].capitalize()
    print '2', name
    else:
    name[0]= name[0].capitalize()
    print '3', name

    I get the desired result.

    Colin W.
     
    Colin J. Williams, Jul 12, 2005
    #1
    1. Advertising

  2. Colin J. Williams

    Robert Kern Guest

    Colin J. Williams wrote:
    > The snippet of code below gives the result which follows
    >
    > for k in ut.keys():
    > name= k.split('_')
    > print '\n1', name
    > if len(name) > 1:
    > name[0]= name[0] + name[1].capitalize()
    > print '2', name
    > name[0]= name[0].capitalize()
    > print '3', name
    >
    > 1 ['logical', 'or']
    > 2 ['logicalOr', 'or']
    > 3 ['Logicalor', 'or']
    >
    > I was expecting that 3 would read ['LogicalOr', 'or']


    str.capitalize() changes the first character to be uppercase and all
    later characters to be lower case. It does not leave the later
    characters alone.

    In [1]: str.capitalize?
    Type: method_descriptor
    Base Class: <type 'method_descriptor'>
    String Form: <method 'capitalize' of 'str' objects>
    Namespace: Python builtin
    Docstring:
    S.capitalize() -> string

    Return a copy of the string S with only its first character
    capitalized.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jul 12, 2005
    #2
    1. Advertising

  3. On Mon, 11 Jul 2005 22:10:33 -0400, "Colin J. Williams" <> wrote:

    >The snippet of code below gives the result which follows
    >
    >for k in ut.keys():
    > name= k.split('_')
    > print '\n1', name
    > if len(name) > 1:
    > name[0]= name[0] + name[1].capitalize()
    > print '2', name
    > name[0]= name[0].capitalize()
    > print '3', name
    >
    >1 ['logical', 'or']
    >2 ['logicalOr', 'or']
    >3 ['Logicalor', 'or']
    >
    >I was expecting that 3 would read ['LogicalOr', 'or']
    >
    >If I replace the above code with:
    >
    >for k in ut.keys():
    > name= k.split('_')
    > print '\n1', name
    > if len(name) > 1:
    > name[0]= name[0].capitalize() + name[1].capitalize()
    > print '2', name
    > else:
    > name[0]= name[0].capitalize()
    > print '3', name
    >
    >I get the desired result.
    >

    If you walk through the results, you can see what happens to name[2] on output line 2:

    >>> 'logicalOr'.capitalize()

    'Logicalor'

    I.e.,
    >>> help(str.capitalize)

    Help on method_descriptor:

    capitalize(...)
    S.capitalize() -> string

    Return a copy of the string S with only its first character
    capitalized. ^^^^-- meaning all the rest lowercased,
    which changed your trailing 'Or'

    So, doing .capitalize on all the pieces from split('_') and then joining them:

    >>> def doit(w): return ''.join([s.capitalize() for s in w.split('_')])

    ...
    >>> doit('logical_or')

    'LogicalOr'
    >>> doit('logical')

    'Logical'
    >>> doit('logical_or_something')

    'LogicalOrSomething'
    >>> doit('UP_aNd_down')

    'UpAndDown'

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 12, 2005
    #3
  4. Bengt Richter wrote:
    > On Mon, 11 Jul 2005 22:10:33 -0400, "Colin J. Williams" <> wrote:
    >
    >
    >>The snippet of code below gives the result which follows
    >>
    >>for k in ut.keys():
    >> name= k.split('_')
    >> print '\n1', name
    >> if len(name) > 1:
    >> name[0]= name[0] + name[1].capitalize()
    >> print '2', name
    >> name[0]= name[0].capitalize()
    >> print '3', name
    >>
    >>1 ['logical', 'or']
    >>2 ['logicalOr', 'or']
    >>3 ['Logicalor', 'or']
    >>
    >>I was expecting that 3 would read ['LogicalOr', 'or']
    >>
    >>If I replace the above code with:
    >>
    >>for k in ut.keys():
    >> name= k.split('_')
    >> print '\n1', name
    >> if len(name) > 1:
    >> name[0]= name[0].capitalize() + name[1].capitalize()
    >> print '2', name
    >> else:
    >> name[0]= name[0].capitalize()
    >> print '3', name
    >>
    >>I get the desired result.
    >>

    >
    > If you walk through the results, you can see what happens to name[2] on output line 2:
    >
    > >>> 'logicalOr'.capitalize()

    > 'Logicalor'
    >
    > I.e.,
    > >>> help(str.capitalize)

    > Help on method_descriptor:
    >
    > capitalize(...)
    > S.capitalize() -> string
    >
    > Return a copy of the string S with only its first character
    > capitalized. ^^^^-- meaning all the rest lowercased,
    > which changed your trailing 'Or'
    >
    > So, doing .capitalize on all the pieces from split('_') and then joining them:
    >
    > >>> def doit(w): return ''.join([s.capitalize() for s in w.split('_')])

    > ...
    > >>> doit('logical_or')

    > 'LogicalOr'
    > >>> doit('logical')

    > 'Logical'
    > >>> doit('logical_or_something')

    > 'LogicalOrSomething'
    > >>> doit('UP_aNd_down')

    > 'UpAndDown'
    >
    > Regards,
    > Bengt Richter

    Many thanks. I missed the implication that any upper case characters
    after the first are changed to lower case.

    Colin W.
     
    Colin J. Williams, Jul 12, 2005
    #4
    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. Mark Andrew

    VHDL: puzzled beginner

    Mark Andrew, Jun 27, 2004, in forum: VHDL
    Replies:
    5
    Views:
    650
    Mark Andrew
    Jun 30, 2004
  2. Daniel

    still puzzled

    Daniel, Jun 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    663
    Steven Cheng[MSFT]
    Jun 9, 2004
  3. schiefaw

    Very Puzzled, Please Help

    schiefaw, Jul 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    388
    schiefaw
    Jul 22, 2005
  4. Replies:
    5
    Views:
    1,410
  5. EvelynAnd Ethan
    Replies:
    4
    Views:
    998
    =?Utf-8?B?Sko=?=
    Jan 9, 2006
Loading...

Share This Page