faqts : Computers : Programming : Languages : Python

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 5 people (60%) answered Yes
Recently 3 of 5 people (60%) answered Yes

Entry

how to convert a string like "saint john's" to "Saint John's"?

Nov 30th, 2008 19:55
Scott Mandarich, Brianna Laugher, Steve Hyatt, matt wilkie, http://funbox.vmgo.com/bookmarks/online-video.html


Here is a quick-and-dirty solution:
def capstring(buf):
    result = buf[0].capitalize()
    for i in range(1, len(buf)):
        if buf[i - 1] == ' ':
            result += buf[i].capitalize()
        else:
            result += buf[i]
    return result
buf = "saint john's"
cap = capstring(buf)
print buf
print cap
Or....!
str.title()