string模块中的capwords()函数功能:代码 :
import sys sys.path.append("C:\Python27\Lib") from string import * s='AAa dwEf sldfji' print capwords(s)
输出:
Aaa Dwef Sldfji
函数原型(源代码解读):
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
def capwords(s, sep=None): """capwords(s [,sep]) -> string Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words. """ return (sep or ' ').join(x.capitalize() for x in s.split(sep))