2022年 11月 5日

python中title函数的作用

我们在python编程中,有的时候需要写标题,但是代码格式为字符串。这时就要用到python中的title函数,它可以将首字母大写,将字符串转换为标题格式。

1、title函数

python中字符串函数,返回’标题化‘的字符串,就是单词的开头为大写,其余为小写

2、语法

string.title()
  • 1

3、使用实例

在这里插入代码片

str=' tHe wOrLDwidE Web '

 

str.title()

 

str.title().strip()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

输出

' The Worldwide Web '

 

'The Worldwide Web'
  • 1
  • 2
  • 3
  • 4
  • 5

以上就是python中title函数的介绍,大家如果需要将字符串转换为标题格式,可以使用title函数哦~