首页 > 代码库 > python 数据类型 ---字符串

python 数据类型 ---字符串

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

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

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

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

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

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

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

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

5.两种字符串格式化打印形式 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
>>>

6. 字符串的判断

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

 

python 数据类型 ---字符串