Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the case of first letter of a string?

s = ['my', 'name'] 

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

s = ['My', 'Name'] 
like image 766
yyyy Avatar asked Nov 19 '10 10:11

yyyy


2 Answers

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:] 
like image 127
Per Mejdal Rasmussen Avatar answered Sep 23 '22 02:09

Per Mejdal Rasmussen


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'] 
like image 25
Frédéric Hamidi Avatar answered Sep 19 '22 02:09

Frédéric Hamidi