码迷,mamicode.com
首页 > 其他好文 > 详细

string.capwords()函数

时间:2016-06-11 15:54:17      阅读:2308      评论:0      收藏:0      [点我收藏+]

标签:

string.capwords()函数

string.capwords()函数,有需要的朋友可以参考下。


代码 :

import syssys.path.append("C:/Python27/Lib")from string import *s=‘AAa dwEf sldfji‘print capwords(s)

输出:

Aaa Dwef Sldfji

string模块中的capwords()函数功能:1.将每个单词首字母置为大写2.将每个单词除首字母外的字母均置为小写;3.将词与词之间的多个空格用一个空格代替4.其拥有两个参数,第二个参数用以判断单词之间的分割符,默认为空格。

函数原型(源代码解读):

# 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))

 

string.capwords()函数

标签:

原文地址:http://www.cnblogs.com/yymn/p/5575383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!