3

I have string:

n = 'my fancy extension'

with

  ''.join([ x.capitalize() for x in n.split() ])

I get MyFancyExtension, but I need myFancyExtension.

How do I avoid capitalizing the first item in list, or not capitalizing at all if one word only specified?

6 Answers 6

3
>>> n = 'my fancy extension'
>>> array = n.split()
>>> array[0] + ''.join(map(str.capitalize, array[1:]))
'myFancyExtension'
Sign up to request clarification or add additional context in comments.

5 Comments

n="onewordwithnospace"
@meshy very elegant solution! so, there is nothing in python so I could say ''.join([ x.capitalize() for x in l --if x not first elmnt-- ]), part between -- being some index test?
Thank you! There may well be, but I can't think of it at the moment. If something occurs to me I'll surely update this :)
I've posted just such a solution below :) Python's ternary can be applied to the 'x for x in my_iterable' list comprehension in a few different places.
Yeah, that does the trick! I'm going to hold out for something more terse though :)
3

In python 3 you can do:

n = 'my fancy extension'

first,*rest = n.split()

''.join([first] + [x.capitalize() for x in rest])
Out[10]: 'myFancyExtension'

python 2 doesn't have the fancy extended tuple unpacking syntax, so you'd need an extra step (oh no!):

spl = n.split()

first,rest = spl[0],spl[1:]

1 Comment

elegant also, just not so nice with python2, and thats what I'm using. thanks :)
3

Not a fan of dense one-liners, but:

>>> words = "my fancy extension"
>>> ''.join([x.capitalize() if i else x for (i, x) in enumerate(words.split())])
'myFancyExtension'

Comments

2

so, there is nothing in python so I could say ''.join([ x.capitalize() for x in l --if x not first elmnt-- ]), part between -- being some index test?

Actually you can use Python's ternary to achieve such an effect:

x if Condition else y

Or, in this case,

x.capitalize() if Condition else x.lower()

For example:

>>> def camel(my_string):
...     my_list = my_string.split()
...     return ''.join([x.lower() if x is my_list[0] else x.capitalize() for x in my_list])
>>> camel("lovely to be here")
'lovelyToBeHere'

Where 'x' is a string piece, 'condition' is if x is my_list[0], and the two options are of course x.lower() and x.capitalize().

Also kind of nice because if you goof up the first part it will lowercase it for you :)

>>> camel("WHat is the problem")
'whatIsTheProblem'
>>> 

In most languages it's written as if Condition x else y order, instead of x if Condition else y so a lot of Python folk shy away from it, but personally I think Python's ternary is cool.

1 Comment

nicely explained, I will have that in mind for a future tasks. ;)
1
l = n.split()
if len(l)>1:
  print l[0] + ''.join([ x.capitalize() for x in l[1:] ])
else:
  print l[0]

or

import re
print re.sub(" ([a-z])", lambda s: s.group(1).upper(), n)

2 Comments

is it possible to have some more idiomatic way of doing that? anyway thanks for your quick answer.
please define idiomatic
1
n = 'my fancy extension'

n = n.title()
n = n.replace(" ", "")
n = n[0].lower() + n[1:]

print(n)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.