[Tutor] string case manipulation in python 3.x

Wayne Werner waynejwerner at gmail.com
Sun Nov 28 07:39:36 CET 2010

On Sat, Nov 27, 2010 at 10:55 PM, 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.

So for instance, something like this?

In [3]: badcasename = 'eRic SPAM IDle jr.'

In [4]: badcasename.title()
Out[4]: 'Eric Spam Idle Jr.'

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.

If you want it to behave differently for suffixes, I'm not sure how to do

> I'm not entirely sure how to replace the string with an uppercase
> first letter on a per word basis.

mystring = mystring.title()

> Whats the "right" way to do something like this?

gaining more familiarity with the builtin functions ;)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101128/1f70502b/attachment-0001.html>

More information about the Tutor mailing list