Python string 模块中的 capwords()方法介绍

语法

Syntax: string.capwords(string[, sep=None])
Return Value: Returns a formatted string after above operations.

描述

缺省 sep 参数的情况下

capwords() 方法用来将传入的字符串用 split() 按照空格分隔成序列后,将序列每项首字母大写后,再使用空格组成字符串返回。

指定 sep 参数的情况下

split() 函数会使用传递给 sep 的参数进行拆分和组和传入的字符串。

代码示例

# imports string module 
import string 
sentence = 'Python is one of the best programming languages.'
# sep parameter is left None 
formatted = string.capwords(sentence, sep = None) 
print(formatted) // Python Is One Of The Best Programming Languages.

# sep parameter is 'o' 
formatted = string.capwords(sentence, sep = 'o') 
print('When sep = "o"', formatted) // When sep = "o" PythoN is oNe oF the best proGramming languages.

发布了89 篇原创文章 · 获赞 83 · 访问量 3492

猜你喜欢

转载自blog.csdn.net/devin_xin/article/details/105394311