I need to capitalize the first letter of every word in a string using regular expressions:
>>> import re
>>> re.sub(r"\b(\S)", (r"\1").upper(), "foo bar")
'foo bar'
I expect the result to be 'Foo Bar' instead.
I have tried the title method and string.capwords, but both have issues as shown below:
>>> import string
>>> string.capwords("foo bar 1a ")
'Foo Bar 1a'
>>> "1a".title()
1A
Using the lambda expression as @Sebastian suggests in the comment below worked for me.