[Tutor] Errr...Capitalizing Words

Lindsay Davies Lindsay.Davies@moonshine.co.uk
Fri, 16 Feb 2001 09:05:42 +0000


--============_-1229808104==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

Well, here's a quick line that will do it for you...

import string,re
s = '''this is some sample text. its all in lower case. there are no 
capitals in this block of text. you get the idea, and now i'm just 
typing to make a lot of text.'''

s = re.sub("(\.\s*|^\s*)(\w)", (lambda x: x.group(1) +\
     string.upper(x.group(2))), s)

This matches appropriate characters with a regular expression, and 
then capitalizes them.

Best wishes,

Lindsay


On 15/2/01, Britt Green wrote about '[Tutor] Errr...Capitalizing Words':
>Hello,
>
>I'm kind of stuck on something seemingly trivial, and I'm hoping 
>someone can help me out. Say I have a block of text, all in lower 
>case:
>
>this is some sample text. its all in lower case. there are no 
>capitals in this block of text. you get the idea, and now i'm just 
>typing to make a lot of text.
>
>What I want to do with Python is take the first letter of each 
>sentance and capitalize it. By looking into the string library I've 
>found the string.capitalize() function. This only seems to work if 
>you know the word you're trying to use. Then string.capwords() will 
>capitalize each word instead of just the first one.
>
>Can anyone point me in the right direction for this?
>
>Thanks,
>
>Britt
>
>--
>It is pitch black. You are likely to be eaten by a grue.
>
>_________________________________________________________________
>Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor

--============_-1229808104==_ma============
Content-Type: text/html; charset="us-ascii"

<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { margin-top: 0 ; margin-bottom: 0 }
 --></style><title>Re: [Tutor] Errr...Capitalizing
Words</title></head><body>
<div>Well, here's a quick line that will do it for you...</div>
<div><br></div>
<div><font color="#000000"><b>import</b> string,re</font></div>
<div><font color="#000000">s =</font><font color="#007F00"> '''this is
some sample text. its all in lower case. there are no capitals in this
block of text. you get the idea, and now i'm just typing to make a lot
of text.'''</font></div>
<div><font color="#000000"><br></font></div>
<div><font color="#000000"><b>s =</b> re.sub(</font><font
color="#007F00">&quot;(\.\s*|^\s*)(\w)&quot;</font><font
color="#000000">, (<b>lambda</b> x: x.group(1) +\</font></div>
<div><font color="#000000">&nbsp;&nbsp;&nbsp;
string.upper(x.group(2))), s)</font></div>
<div><br></div>
<div>This matches appropriate characters with a regular expression,
and then capitalizes them.</div>
<div><br></div>
<div>Best wishes,</div>
<div><br></div>
<div>Lindsay</div>
<div><br></div>
<div><br></div>
<div>On 15/2/01, Britt Green wrote about '[Tutor] Errr...Capitalizing
Words':</div>
<blockquote type="cite" cite>Hello,<br>
<br>
I'm kind of stuck on something seemingly trivial, and I'm hoping
someone can help me out. Say I have a block of text, all in lower
case:<br>
<br>
this is some sample text. its all in lower case. there are no capitals
in this block of text. you get the idea, and now i'm just typing to
make a lot of text.<br>
<br>
What I want to do with Python is take the first letter of each
sentance and capitalize it. By looking into the string library I've
found the string.capitalize() function. This only seems to work if you
know the word you're trying to use. Then string.capwords() will
capitalize each word instead of just the first one.<br>
<br>
Can anyone point me in the right direction for this?<br>
<br>
Thanks,<br>
<br>
Britt<br>
<br>
--<br>
It is pitch black. You are likely to be eaten by a grue.<br>
<br>
_________________________________________________________________<br>
Get your FREE download of MSN Explorer at http://explorer.msn.com<br>
<br>
<br>
_______________________________________________</blockquote>
<blockquote type="cite" cite>Tutor maillist&nbsp; -&nbsp;
Tutor@python.org</blockquote>
<blockquote type="cite"
cite>http://mail.python.org/mailman/listinfo/tutor</blockquote>
<div><br></div>
</body>
</html>
--============_-1229808104==_ma============--