[Tutor] string case manipulation in python 3.x

Hugo Arts hugo.yoshi at gmail.com
Sun Nov 28 06:10:10 CET 2010

On Sun, Nov 28, 2010 at 5:55 AM, Rance Hall <ranceh at gmail.com> wrote:
> I need to do some case manipulation that I don't see in the documented
> string functions.
> I want to make sure that user input meets a certain capitalization
> scheme, for example, if user input is a name, then the first letter of
> each word in the name is upper case, and the rest are lower.
> I know how to force the upper and lower cases with string.lower() and friends.
> and I could even do a string.split() on the spaces in the names to
> break the name into pieces.
> I don't see an obvious way to do this.
> what I've come up with so far is to do something like this.
> break the name into pieces
> force each piece to be lower case
> replace the first letter in each word with a uppercase version of
> whats there already.
> Problems with this approach as I see them:
> The built in split function will create undesirable results for names
> that contain suffixes like Jr.  etc.
> I'm not entirely sure how to replace the string with an uppercase
> first letter on a per word basis.
> Whats the "right" way to do something like this?

I'd say split the string, call the capitalize method on each part,
then join back together.


More information about the Tutor mailing list