-1
names = ['one', 'two']
print([n[0].upper() for n in names])

I want output like this: One, Two

how can I do this with python?

2 Answers2

7
>>> names = ['one', 'two']
>>> names = [n.title() for n in names]
>>> names
['One', 'Two']
>>> 

If you want it to work with a capital letter in it like oNe will be ONe then:

>>> names = ['oNe', 'twO']
>>> names = [n[0].upper()+n[1:] if n else "" for n in names]
>>> names
['ONe', 'TwO']
>>> 
Nouman
  • 6,947
  • 7
  • 32
  • 60
1

You need to use the method 'capitalize()' of the Python Standard Library.Here is the code :

names = ['one', 'two']
print([n.capitalize() for n in names])

So, you will get this output: ['One', 'Two']

codrelphi
  • 1,075
  • 1
  • 7
  • 13