python capitalize first letter only

Only because no one else has mentioned it: >>> ‘bob’.title() ‘Bob’ >>> ‘sandy’.title() ‘Sandy’ >>> ‘1bob’.title() ‘1Bob’ >>> ‘1sandy’.title() ‘1Sandy’ However, this would also give >>> ‘1bob sandy’.title() ‘1Bob Sandy’ >>> ‘1JoeBob’.title() ‘1Joebob’ i.e. it doesn’t just capitalize the first alphabetic character. But then .capitalize() has the same issue, at least in that ‘joe Bob’.capitalize() … Read more

Regex capitalize first letter every word, also after a special character like a dash

+1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well: var re = /(\b[a-z](?!\s))/g; var s = “fort collins, croton-on-hudson, harper’s ferry, coeur d’alene, o’fallon”; s = s.replace(re, function(x){return x.toUpperCase();}); console.log(s); // “Fort Collins, Croton-On-Hudson, Harper’s Ferry, Coeur D’Alene, O’Fallon”