Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
62
5
>>> word = 'the'
>>> poem.count(word)
3
是否这首诗中的所有字符要么是字母,要么是数字?
>>> poem.isalnum()
False
不,还有一些标点符号。
5.14
大小写
在本节中,再来看一些内建字符串函数的用法。测试字符串还是之前那个:
>>> setup = 'a duck goes into a bar...'
删除两侧的
.
>>> setup.strip('.')
'a duck goes into a bar'
因为字符串是不可变的,所以这些示例其实都没改变过
setup
字符串。它们
只是获取
setup
的值,对其执行操作,将结果作为新字符串返回。
第一个单词首字母大写:
>>> setup.capitalize()
'A duck goes into a bar...'
所有单词的首字母都大写:
>>> setup.title()
'A Duck Goes Into A Bar...'
将所有字母都转换成大写:
>>> setup.upper()
'A DUCK GOES INTO A BAR...'
将所有字母都转换成小写:
>>> setup.lower()
'a duck goes into a bar...'
交换字母大小写。
>>> setup.swapcase()
'A DUCK GOES INTO A BAR...'
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223