0

I have a list containing multiple stings. The strings all start with a "*" and then a "[" with a space in between. Like this:

* [context......

Now i would like to capitalize "c". In other words, i would like to capitalize the first letter of all the strings. But since i use symbols first. the Capitalize() function wont work.

What i have tried is to capitalize the index like this:

list = [i[3].capitalize() for i in list]

The output of this, is just the capitalized letters. And not the rest of the string.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
kevinSmith
  • 23
  • 4

4 Answers4

2

The easiest way is to use .title() which ignores non-alphabetical characters:

>>> my_list = ['* [foo]', '* [bar]']
>>> map(lambda s: s.title(), my_list)
['* [Foo]', '* [Bar]']

or using a list comprehension:

>>> [s.title() for s in my_list]
['* [Foo]', '* [Bar]']
Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

If they all start that way, then I think this would do what you want.

list_ = [i[:3] + i[3:].capitalize() for i in list_]

Note that you shouldn't use the built in keyword list as a variable name (it covers over its purpose in the code).

Chris Charley
  • 6,403
  • 2
  • 24
  • 26
1

TRY:-

list1 = [ "* [context......", "* [context1......" ]

for x in list1:

    print(x[:3] + x[3].capitalize() + x[4:])

OUTPUT:-

* [Context......
* [Context1......
Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
1

Here's an example using upper() instead of capitalize. Reference from here

test = ['* [context......', '* [bontext......', '* [zontext......']

test = [i[:3] + i[3:4].upper() + i[4:] for i in test]
Simon
  • 1,201
  • 9
  • 18