Puzzled

C

Colin J. Williams

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.
 
R

Robert Kern

Colin said:
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
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
B

Bengt Richter

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'

I.e., 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
 
C

Colin J. Williams

Bengt said:
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'

I.e., 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top