33
s = ['my', 'name']

I want to change the 1st letter of each element in to Upper Case.

s = ['My', 'Name']
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
yyyy
  • 347
  • 1
  • 3
  • 3

5 Answers5

56

Both .capitalize() and .title(), changes the other letters in the string to lower case.

Here is a simple function that only changes the first letter to upper case, and leaves the rest unchanged.

def upcase_first_letter(s):
    return s[0].upper() + s[1:]
30

You can use the capitalize() method:

s = ['my', 'name']
s = [item.capitalize() for item in s]
print s  # print(s) in Python 3

This will print:

['My', 'Name']
trss
  • 915
  • 1
  • 19
  • 35
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
14

You can use 'my'.title() which will return 'My'.

To get over the complete list, simply map over it like this:

>>> map(lambda x: x.title(), s)
['My', 'Name']

Actually, .title() makes all words start with uppercase. If you want to strictly limit it the first letter, use capitalize() instead. (This makes a difference for example in 'this word' being changed to either This Word or This word)

Frank
  • 10,461
  • 2
  • 31
  • 46
6

It probably doesn't matter, but you might want to use this instead of the capitalize() or title() string methods because, in addition to uppercasing the first letter, they also lowercase the rest of the string (and this doesn't):

s = map(lambda e: e[:1].upper() + e[1:] if e else '', s)

Note: In Python 3, you'd need to use:

s = list(map(lambda e: e[:1].upper() + e[1:] if e else '', s))

because map() returns an iterator that applies function to every item of iterable instead of a list as it did in Python 2 (so you have to turn it into one yourself).

martineau
  • 119,623
  • 25
  • 170
  • 301
  • +1: nice catch; however, why the `if s else ''` expression? To avoid a concatenation of empty strings? I believe it's premature optimization. – tzot Nov 20 '10 at 21:31
  • @ΤΖΩΤΖΙΟΥ: Thanks. That part is there mainly because I just tweaked my answer to a similar [question](http://stackoverflow.com/questions/3840843/how-to-downcase-the-first-character-of-a-string-in-python/3847369#3847369). I prefer to think of it as defensive programming since it can handle a list element being `None` (so, no, it's not there because of a concerned about the empty string case being less than optimal). – martineau Nov 21 '10 at 00:12
  • 1
    I think handling `None` like this is bad practice. I would rather have an exception raised in this case than have the (possibly wrong) blank string silently passed. – dbkaplun Aug 13 '12 at 20:52
  • @MindVirus: If you want an exception for `None` values you could use `lambda e: e[:1].upper() + e[1:] if e or e is None else ''`. – martineau Aug 13 '12 at 22:02
  • 2
    @martineau, my whole gripe is that you're attempting to circumvent duck typing. The correct solution, in my opinion, is `lambda e: e[:1].upper() + e[1:]`. – dbkaplun Aug 15 '12 at 15:05
  • 1
    @virtualxtc: If you look the original question, you'd see that `s` is the list of strings being modified. The `e` is just the argument name of one of the elements from it that the `map()` function will pass to the `lambda` function which is defined in the expression. – martineau May 31 '18 at 13:36
  • Ok, thanks, the variables are more clear enough now (it's my 4th day using python). However, I'm intrigued, but confused by this discussion about `e is None`. – virtualxtc May 31 '18 at 20:25
  • 1
    @virtualxtc: It has to do with the handling of any elements of the list (expected to all be strings) that are empty `""` or have the value `None`. Both of those kind of values have a truthiness of `False` in Python when they're used as a simple boolean/conditional value like the expression in an `if` statement. There's also a related issue being discussed about whether values like that should be dealt-with silently/automatically or raise exceptions should one be encountered. – martineau Jun 01 '18 at 01:09
0

You can use

for i in range(len(s)):
   s[i]=s[i].capitalize()
print s
Kracekumar
  • 19,457
  • 10
  • 47
  • 56
  • 1
    You don't need `temp`, it doesn't doing anything and so is the same as `list[i]=list[i].capitalize()` -- which would only work if the OP's list named `s` was renamed `list` -- but that would be a bad practice because doing so would hide the built-in function/type of the same name. Not sure why the trailing comma on the print statement was put there... – martineau Nov 19 '10 at 11:41
  • it was typo mistake to put comma there. – Kracekumar Nov 19 '10 at 11:50