python单词首字母大写

Here, we are implementing a python program to capitalizes the first letter of each word in a string.

在这里,我们正在实现一个python程序来大写字符串中每个单词的首字母。

Example:

例:

    Input: "HELLO WORLD!"
Output: "Hello World!"

Method 1: Using title() method

方法1:使用title()方法

# python program to capitalizes the
# first letter of each word in a string
# function
def capitalize(text):
return text.title()
# main code
str1 = "Hello world!"
str2 = "hello world!"
str3 = "HELLO WORLD!"
str4 = "includehelp.com is a tutorials site"
# printing
print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)
print("str4: ", str4)
print()
print("capitalize(str1): ", capitalize(str1))
print("capitalize(str2): ", capitalize(str2))
print("capitalize(str3): ", capitalize(str3))
print("capitalize(str4): ", capitalize(str4))

Output

输出量

str1:  Hello world!
str2:  hello world!
str3:  HELLO WORLD!
str4:  includehelp.com is a tutorials site
capitalize(str1):  Hello World!
capitalize(str2):  Hello World!
capitalize(str3):  Hello World!
capitalize(str4):  Includehelp.Com Is A Tutorials Site

Method 2: Using loop, split() method

方法2:使用循环,split()方法

# python program to capitalizes the
# first letter of each word in a string
# function
def capitalize(text):
return  ' '.join(word[0].upper() + word[1:] for word in text.split())
# main code
str1 = "Hello world!"
str2 = "hello world!"
str3 = "HELLO WORLD!"
str4 = "includehelp.com is a tutorials site"
# printing
print("str1: ", str1)
print("str2: ", str2)
print("str3: ", str3)
print("str4: ", str4)
print()
print("capitalize(str1): ", capitalize(str1))
print("capitalize(str2): ", capitalize(str2))
print("capitalize(str3): ", capitalize(str3))
print("capitalize(str4): ", capitalize(str4))

Output

输出量

str1:  Hello world!
str2:  hello world!
str3:  HELLO WORLD!
str4:  includehelp.com is a tutorials site
capitalize(str1):  Hello World!
capitalize(str2):  Hello World!
capitalize(str3):  HELLO WORLD!
capitalize(str4):  Includehelp.com Is A Tutorials Site

翻译自: https://www.includehelp.com/python/capitalizes-the-first-letter-of-each-word-in-a-string.aspx

python单词首字母大写

python单词首字母大写_在Python中将每个单词的首字母大写相关推荐

  1. python字符串大写字母个数_【python实例】统计字符串里大写字母,小写字母的个数和非字母的个数...

    """ 给定一个以下字符串:统计大写字母的个数,小写字母的个数,非字母的个数. str1 = "ajdkkKDKEK1343KFKiriromfkfKKRIOW ...

  2. python输出自己的名字_【Python】Python-用大写字母打印你的名字

    http://www.cnblogs.com/xiashengwang/p/4294409.html 与孩子一起编程,有个作业题:用大写字母打印你的名字. CCCC A RRRRR C C A A R ...

  3. python统计元音字母个数_计算Python中的元音(Counting vowels in python)

    计算Python中的元音(Counting vowels in python) def main(): print(count) def countVowels(string): vowel=(&qu ...

  4. python单词软件哪个好_还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!...

    学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...

  5. python单词软件哪个好_利用Python分析背单词软件的惊人真相

    摘要:利用python分析背单词软件,揭秘你不知道的惊人真相 0×00 前言 你想知道背单词软件有大概多少人注册第一天都没有背完嘛 你想知道背单词软件这么火,这么多人在使用,真的有多少人真的在背诵嘛 ...

  6. python十大必备知识_学Python必备的基础知识

    学Python必备的基础知识 1.基本概念 表达式:就是一个类似于数学公式的东西,一般仅仅用了计算一些结果 ,不会对程序产生实质性的影响,如9+3; 语句:在程序中语句一般需要完成某种功能,比如打印信 ...

  7. 88是python语言的整数类型_少儿Python编程_第三讲:常量变量和数据类型

    无论使用哪一种编程语言,甚至是学习数学.物理,都需要掌握常量.变量.表达式的概念和用法.本讲将带领读者进入编程世界,学习程序的基本元素. 3.1 基本数据类型 基本数据类型有数值型.布尔型和字符型.它 ...

  8. python进阶之学习笔记_干货 | Python进阶系列之学习笔记(二)

    目录 对象 字符串 一.对象 (1)什么是对象 在python中一切都是对象,每个对象都有三个属性分别是,(id)身份,就是在内存中的地址,类型(type),是int.字符.字典(dic).列表(li ...

  9. python文本结构化处理_在Python中标记非结构化文本数据

    python文本结构化处理 Labelled data has been a crucial demand for supervised machine learning leading to a n ...

  10. python树莓派编程 沃尔弗拉姆_《 Python树莓派编程》——3.3 Python入门-阿里云开发者社区...

    本节书摘来自华章出版社<Python树莓派编程>一书中的第3章,第3.3节,作者:[美]沃尔弗拉姆·多纳特(Wolfram Donat)著韩德强 等译,更多章节内容可以访问云栖社区&quo ...

最新文章

  1. 提高PHP编程效率的53个要点
  2. 【Storm篇】--Storm基础概念
  3. 使用ML.NET + Azure DevOps + Azure Container Instances打造机器学习生产化
  4. 【渝粤题库】国家开放大学2021春3897商务英语1题目
  5. 华科计算机复试ccf成绩,新鲜速递!刚拟录取华科学妹总结的复试攻略,快看
  6. 桔子浏览器电脑版收藏夹位置在哪里 收藏夹位置路径
  7. 安卓9.0系统新特性
  8. bootstrap table 光标_第三章之Bootstrap 表格与按钮功能
  9. Understand-4.0.877-Linux-64bit.tgz最新版本2017年源代码阅读利器,养眼theme之配置
  10. Canon EOS Utility安装和使用说明
  11. Tomcat介绍和MyEclipse搭建DRP系统
  12. python 使用ThreadPool 创建线程,后台执行并check
  13. Linux C编程(五) 之 gdb详解
  14. 中科院计算机技术研究所张浩,中国科学院计算技术研究所 韩 琥 博士
  15. 电脑无法安装软件打不开计算机,电脑刚安装软件打不开怎么办
  16. Java八股文--藤原豆腐店自用
  17. 写毕业论文更新目录时,如何让格式不会变?
  18. Direct3DCreate9与Direct3DCreate9Ex
  19. 搭建IIS文件服务器
  20. linux终端设备:tty子系统相关的初始化

热门文章

  1. mac php命令行模式,phpstorm分别在Mac和Windows下启动命令行,并启用ssh
  2. freemark循环map_java与freemarker遍历map
  3. datatables 行分组信息展开与折叠的功能实现_[LaTeX 尝试] fancyvrb - 修复行引用的超链接跳转位置
  4. Python二级笔记(16)
  5. 你用计算机做过什么事情,他偷偷干了什么?(电脑)
  6. netty获取玩家chanel_Netty4.X 获取客户端IP
  7. Springboot 整合 swagger
  8. 游戏外挂是怎么炼成的
  9. cnblogs修改网站图标icon
  10. Spark 独立部署模式