Python Forum

Full Version: make a list of string in capital by using a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


hi,thanks for looking my post

for now i am able to fulfill it through a lambada funtion:

l = ['java','python','c++'] #a list of string
a = list(map(lambda x: x.title() ,l)) #using map and title funtion upper every first letter
a

anyway,i really want to know how to achieve it without lambda funtion,like define a funtion first then using the 'map'
i tried but none of these works

def title1(x): #define a function name title1
c=x.title()
return c

can anyone point out what;s the matter with my code?thanks
you can load the package titlestring, and then:
>>> from titlecase import titlecase
>>> titlecase('i want title case on this string')
'I Want Title Case on This String'
>>>
list comp
>>> l = ['java','python','c++'] 
>>> [x.title() for x in l]
['Java', 'Python', 'C++']
@ananbc
You should avoid filter, map and reduce.
Take "List Comprehension"
See https://www.python-course.eu/list_comprehension.php