Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: change the first letter into uppercase (ask)

33 views
Skip to first unread message

Dave Angel

unread,
Oct 20, 2012, 12:21:14 AM10/20/12
to contro opinion, python-list
On 10/20/2012 12:09 AM, contro opinion wrote:
> how can i use regular expression in python to change the string
> "a test of capitalizing"
> into
> "A Test Of Capitalizing"?
>
>

is that (regex) part of the assignment, or just how you assumed you'd
want to do it?

There is a str method already called title(). Check to see if that's
what you want.



--

DaveA

Zero Piraeus

unread,
Oct 20, 2012, 1:03:46 AM10/20/12
to python-list
:

On 20 October 2012 00:09, contro opinion <contro...@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
>>> for match in re.findall(pattern, "a test of capitalizing"):
... result = f(result + match)
...
>>> print(result)
A Test Of Capitalizing
>>>

-[]z.
Message has been deleted

Ian Kelly

unread,
Oct 20, 2012, 7:39:41 PM10/20/12
to Python
On Sat, Oct 20, 2012 at 5:14 PM, contro opinion <contro...@gmail.com> wrote:
> the pattern `re.compile(".(?#nyh2p){0,1}")` , make me confused,
> can you explain how it can match the first letter of every word?

It doesn't.

>>> pattern = re.compile(".(?#nyh2p){0,1}")
>>> pattern.findall("a test of capitalizing")
['a', ' ', 't', 'e', 's', 't', ' ', 'o', 'f', ' ', 'c', 'a', 'p', 'i',
't', 'a', 'l', 'i', 'z', 'i', 'n', 'g', '']

The regex is just convolution. The solution actually works by calling
str.title(). The fact that it calls it 23 times instead of once is
further convolution.

MRAB

unread,
Oct 20, 2012, 7:43:42 PM10/20/12
to pytho...@python.org
On 2012-10-21 00:14, contro opinion wrote:
> the pattern `re.compile(".(?#nyh2p){0,1}")` , make me confused,
> can you explain how it can match the first letter of every word?
>
It matches all of the characters, plus an empty string at the end. It's
equivalent to:

result = ""

for c in "a test of capitalizing":
result = (result + c).title()

result = (result + "").title()

or:

result = "a test of capitalizing".title()

but somewhat slower...

>
> 2012/10/20 Dennis Lee Bieber <wlf...@ix.netcom.com
> <mailto:wlf...@ix.netcom.com>>
>
> On Sat, 20 Oct 2012 01:03:46 -0400, Zero Piraeus <sch...@gmail.com
> <mailto:sch...@gmail.com>>
> declaimed the following in gmane.comp.python.general:
>
> > :
> >
> > On 20 October 2012 00:09, contro opinion <contro...@gmail.com
> <mailto:contro...@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'
> >>>
>

Ian Kelly

unread,
Oct 20, 2012, 7:44:04 PM10/20/12
to Python
On Sat, Oct 20, 2012 at 1:30 PM, Dennis Lee Bieber
<wlf...@ix.netcom.com> wrote:
>> >>> 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()

These result in a string that is all-caps, not title-case.
Message has been deleted
Message has been deleted

Zero Piraeus

unread,
Oct 21, 2012, 4:22:08 AM10/21/12
to pytho...@python.org
:

On 20 October 2012 20:22, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
> Based on the documentation, most of that pattern is fluff: (?#...)
> is considered a comment.

The comment isn't entirely fluff ... it provides a Google target for
whoever's marking OP's assignment, should they choose to look it up:

https://www.google.com/search?q=nyh2p

-[]z.
0 new messages