Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31817

Re: change the first letter into uppercase (ask)

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: change the first letter into uppercase (ask)
Date 2012-10-20 15:30 -0400
Organization > Bestiaria Support Staff <
References <CA+YdQ_5DAp88HJiQnW9ejDuQGomW5X_b1dUP_sygJB+7s_9iyg@mail.gmail.com> <CANaSqUfHejaD-esy8o573KfZ1UJsNTbw4ziq0DN4ehEjU0ACJw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2565.1350761419.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, 20 Oct 2012 01:03:46 -0400, Zero Piraeus <schesis@gmail.com>
declaimed the following in gmane.comp.python.general:

> :
> 
> On 20 October 2012 00:09, contro opinion <contropinion@gmail.com> wrote:
> > how can i use  regular expression  in  python  to change the string
> > "a test of capitalizing"
> > into
> > "A Test Of Capitalizing"?
> 
> >>> import re
> >>> pattern = re.compile(".(?#nyh2p){0,1}")
> >>> result = ""
> >>> f = str.title

	f = str.capitalize

> >>> for match in re.findall(pattern, "a test of capitalizing"):
> ...   result = f(result + match)

	result = result + f(match)

	Or closer... Don't both with f and str.capitalize

	result = result + match.capitalize()

> ...
> >>> print(result)
> A Test Of Capitalizing
> >>>
> 
	

>>> "a test of capitalizing".title()
'A Test Of Capitalizing'
>>> 

	Not to be confused with

>>> "a test of capitalizing".capitalize()
'A test of capitalizing'
>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: change the first letter into uppercase (ask) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-20 15:30 -0400

csiph-web