栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python字符串函数系列(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python字符串函数系列(一)

#将首字母大写
s1 = "hello world"
s2 = str1.capitalize()
s2
'Hello world'
#去除大小写差异,统一变为小写
s1 = "AbcDR"
s1.casefold()
'abcdr'
#将某个字符串补长至多少位,源字符串位于中间
s1 = "abcBBfgh"
s2 = s1.center(20,'*')
s2
'******abcBBfgh******'
#返回某个子字符串的个数
s1 = "abcBBfgh"
s2 = s1.count('cB')
s2
1
#将字符串编码,默认 utf-8
s1 = "hello python"
s2 = s1.encode()
type(s2)
bytes
#判断某字符串是否以某后缀结尾
s1 = "hello python"
s2 = s1.endswith('n')
s2
True
#将 't' 用若干空格代替,默认是8
s1 = "hello pythont"
s2 = s1.expandtabs(4)
s2
'hello python    '
#返回子字符串第一次出现的位置,如果没有,则返回-1
s1 = "hello world"
s2 = s1.find('wd')
s2
-1
#返回子字符串第一次出现的位置,如果没有,会报错
s1 = "hello world"
s2 = s1.index('wo')
s2
6
#判断某字符串是否为字母或数字组成的字符串
s1 = "5656"
s2 = s1.isalnum()
s2
True
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844387.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号