Hi,

I have another question: ¿is there a method that allows me to capitalize the first letter of EVERY WORD in a given string?

I know that something like:

a = "hello world"
print(a.capitalize())

would print "Hello world"

but I need it to print Hello World. It's for a program that automatically capitalizes book names, and they should be in that format. For instance: "The Book Of My Friends"

Thank you for your help.

You could write this

>>> import re
>>> re.sub(r"\w+", lambda m: m.group(0).capitalize(), "the book of my friends")
'The Book Of My Friends'

But, it's better to compile the regex first

import re
word_re = re.compile("\w+")
def _cap(match):
    return match.group(0).capitalize()

def capitalize_all(s):
    return word_re.sub(_cap, s)

print(capitalize_all("the book of my friends"))
"""my output --->
The Book Of My Friends
"""

Thank you very much Gribouillis, that is exactly what I was looking for. That piece of code covers my needs perfectly.

I would use split, map and join again, if I do not want to keep exact whitespace

>>> a="the book of my dreams"
>>> a.capitalize()
'The book of my dreams'
>>> a.split()
['the', 'book', 'of', 'my', 'dreams']
>>> map(lambda x: x.capitalize(), a.split())
['The', 'Book', 'Of', 'My', 'Dreams']
>>> " ".join(map(lambda x: x.capitalize(), a.split()))
'The Book Of My Dreams'
>>>

I would use split, map and join again, if I do not want to keep exact whitespace

>>> a="the book of my dreams"
>>> a.capitalize()
'The book of my dreams'
>>> a.split()
['the', 'book', 'of', 'my', 'dreams']
>>> map(lambda x: x.capitalize(), a.split())
['The', 'Book', 'Of', 'My', 'Dreams']
>>> " ".join(map(lambda x: x.capitalize(), a.split()))
'The Book Of My Dreams'
>>>

The use of map is deprecated, you should use list comprehension instead

" ".join([x.capitalize() for x in a])

The use of map is deprecated, you should use list comprehension instead

" ".join([x.capitalize() for x in a])

No map is not depracated for Python 3 it returns iterator, so it needs to be passed to list to give same results.

With one more nested function call maybe is not so clear, though.

>>> a="the book of my dreams"
>>> print(list(map(lambda x: x.capitalize(), a.split())))
['The', 'Book', 'Of', 'My', 'Dreams']
>>> print( ' '.join(list(map(lambda x: x.capitalize(), a.split()))))
The Book Of My Dreams

Of cource in your code file you can use multiple lines

print(
    ' '.join(
        list(
            map(lambda x: x.capitalize(),
                a.split())
            )
        )
    )

Edited 6 Years Ago by pyTony: n/a

This capitalization case actually is very good use for simple list comprehension. Just my logic of splitting ... brought first to my mind the map solution.

I see. It is intresting point to keep in mind that list comprehesions beginning is like lambda and if in the end is a filter with lambda (of course it is, but somehow have been little separate in my mind from filter-function, or generator as it may be in Python3)

Must start to appreciate them more.

What has been keeping reserved about them is twisting your mind to produce nested list comprehension (starting to get hang of it) and usually it has been stated that

for example [a]+[b] I have understood to be slower than saying a.append(b) .

Take the following example:

>>> print [abs(i+5) for i in [-5,7,0,-12] if i<5]
[0, 5, 7]

You could express the same thing with filter, maps and lambda:

>>> map( lambda x:abs(x+5), filter(lambda x:x<5 ,[-5,7,0,-12]) )
[0, 5, 7]

The list comprehension is not only easier to read: It's also surprisingly faster.

Edited 6 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.