>> name.strip()Frank字符串的分割, split("分隔符"),分组后成为一个列表>>> name = "Ap">
当前位置:Gxlcms > Python > python数据类型---字符串

python数据类型---字符串

时间:2021-07-01 10:21:17 帮助过:48人阅读

字符串去除空白 ,strip() , 包括空格,tab键, 换行符

>>> name = "  Frank  "
>>> name.strip()
'Frank'

字符串的分割, split("分隔符"),分组后成为一个列表

>>> name = "Apple, banbana, orice"
>>> name.split(",")
['Apple', ' banbana', ' orice']
>>>

字符串的合并 join("连接符")

>>> name = ['Frank', 'Marlon', 'Lee']
>>> "|".join(name)
'Frank|Marlon|Lee'

判断空格是否子字符串中 “in”

>>> name = "Frank Bain"
>>> '' in name
True
>>> "L" in name
False
>>>

两种字符串格式化打印形式 format

# 第一种表示方法
>>> msg = "Hello {name}, your age is {age}"
>>> msg.format(name="Frank", age=24)
'Hello Frank, your age is 24'
>>>

# 第二种表示方法 ,注意index 从0 开始
>>> msg = "Hello{0}, your age is {1}, sex is {2}"
>>> msg.format("Frank", 24, "man")
'HelloFrank, your age is 24, sex is man'
>>>

字符串的判断

>>> name = "Frank"
>>> name.upper()
'FRANK'
>>> name.lower()
'frank'
>>> name.startswith("F")
True
>>> name.endswith("k")
True
>>> name.endswith("a")
False

人气教程排行