string.capwords()函数

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

 

posted @   菜鸡一枚  阅读(955)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2015-06-11 人的差别,在于业余时间
2015-06-11 学点设计
2015-06-11 论文格式与参考文献格式
2015-06-11 低水平博士是提升科研生涯的毒药
2015-06-11 几种方法帮你更有效率的记笔记
2015-06-11 MATLAB——axis
2015-06-11 MATLAB——PLOT绘图
点击右上角即可分享
微信分享提示