Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the gd-system-plugin domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
February 2016 – Eric Scrivner
Fixing Python’s title Function
home // page // Monthly Archives: February, 2016
Technical

Fixing Python’s title Function

Python’s title function has some weird side-effects when you’re titlizing a string like, for example, a street name: >>> "62nd".title() "62Nd" >>> "West 62nd st.".title() "West 62Nd St." Here’s how you can fix this: import re def fixed_title(input_string): parts = re.split(r'\s', input_string) for idx, value in enumerate(parts): if re.match(r'[A-Za-z]', value[0]): parts[idx] = value[0].upper() + value[1:] return ' '.join(parts) A simple fix that produces the right result: >>> fixed_title("West 62nd st.") "West 62nd St."