str 字符串
如何表示字符串?
单行
单引号 '' 如果字符串中有单引号就需要双引号表示,反之亦然
双引号 " "
换行表示 \
one_str = "简洁胜于优雅"
two_str = '简单胜于复杂'
three_str = "做也许好过不做," \
"但不假思索就动手还不如不做"
print(one_str)
print(two_str)
print(three_str)
多行
三引号 ''' ''' 、""" """ 包含引号中的所有内容,用于模块注释、类注释、方法注释
"""
模块注释
"""
class NewClass(object):
"""类注释"""
def stdout(self):
"""方法注释"""
long_str = """扁平胜于嵌套
间隔胜于紧凑
可读性很重要"""
print(long_str)
转义字符 \
不可见但也要录入, enter 和 tab 也是一个输入和动作
one_str ="优美胜于丑陋\n明了胜于晦涩"
# 添加转义字符
two_str = "优美胜于丑\\n明了胜于晦涩"
print(one_str)
print(two_str)
换行符 \n
一个Tab \t
Python IDE 特性 \t \n 并不转义
print() 特性 \t \n 进行转义
\ 意思
1. 只是一行写不下了,另起一行,本质上还是一行
2. 转义字符
转义字符 \
1. 无法"看见"的字符 \n \t \r
2. 与语言本身语法有冲突的字符
换行 \n
回车 \r
单引号 \'
横向制表符 \t
原始字符串,所见即所得,字符串前面 加 r 或 R
one_str =r"优美胜于丑陋\n明了胜于晦涩"
two_str =R"扁平胜于嵌套\n间隔胜于紧凑"
print(one_str)
print(two_str)
字符串方法
a. 大小写转换
字符串第一个字符大写 capitalize
全部小写 lower
将unicode字符小写 casefold
大小写反转 swapcase
空格和标点分割,所有第一个字符大写 title
全部大写 upper
example_str = "Simple is better than complex"
print("capitalize: ", example_str.capitalize())
print("lower: ", example_str.lower())
print("casefold: ", example_str.casefold())
print("swapcase: ", example_str.swapcase())
print("title: ", example_str.title())
print("upper: ", example_str.upper())
a. 字符串填充,如果有 -- 字符填充在这两个 - 中间
居中填充 center
居左填充 ljust
居右填充 rjust
居右填充 0 zfill
指定制表符空格数 expendtabs
格式化 format
字典格式化 format_map
example_str = "Hai!"
print("center: ", example_str.center(10, "-"))
print("rjust: ", example_str.rjust(10, ">"))
print("ljust: ", example_str.ljust(10, "<"))
print("zfill: ", example_str.zfill(10))
two_example_str = "Hai\t{name}!"
print("expandtabs: ", two_example_str.expandtabs(4))
print("format: ", two_example_str.format(name="World"))
print("format_map: ", two_example_str.format_map({"name": "World"}))
c. 字符串统计、定位和替换
统计 count
查找,返回首次找到字符的位置,没找到返回 -1 find
从右边查找 rfind
没找到返 回错误 index
替换字符 replace
去除字符两边指定字符,默认空格和换行 strip
去除左边指定字符 lstrip
去除右边指定字符 rstrip
example_str = " +++ Complex is better than complicated --- "
print("count: ", example_str.count("om"))
print("find: ", example_str.find("om"))
print("rfind: ", example_str.rfind("om"))
print("index: ", example_str.index("om"))
print(": ", example_str.replace("om", "hai", -1))
print("strip: ", example_str.strip("+-"))
print("rstrip: ", example_str.rstrip("- "))
print("ljust: ", example_str.lstrip("+ "))
d. 字符串拼接和分割
指定符号连接可迭代对象各元素 join
指定分隔符,从左分成3部分,保留分隔符 partition
指定分隔符,从右分成3部分,保留分隔符 rpartition
指定分隔符,默认空格和换行,不保留分隔符 split
从右开始分割 rsplit
按换行符进行分割 splitlines
example_str = "Simple is better than complex \n Flat is better than nested"
print("join: ", '-'.join(example_str))
print("partition: ", example_str.partition("is"))
print("rpartition: ", example_str.rpartition("than"))
print("split: ", example_str.split())
print("rsplit: ", example_str.rsplit("is"))
print("splitlines: ", example_str.splitlines())
e. 字符串判断
是否以某个字符开头 startswith
是否以某个字符结尾 endswith
是否全部都是中文、大小字母、数字 isalnum
是否是10进制数字 isdecimal
是否是可命名 isidentifier
是否全部小写 islower
是否全部大写 isupper
是否空白字符(\t \n \r 空格) isspace
example_str = "Simple is better than complex"
print("startswith: ", example_str.startswith("Sim"))
print("endswith: ", example_str.endswith("ley"))
print("isalpha: ", example_str.isalpha())
example_num_str = "11"
print("isdecimal: ", example_num_str.isdecimal())
two_example_str = "01simple"
print("isidentifier: ", two_example_str.isidentifier())
print("islower: ", two_example_str.islower())
print("isupper: ", two_example_str.isupper())
three_example_str = "\n\r\t "
print(three_example_str.isspace())
f. 编码字符 .encode(encoding='utf-8')
解码字符 .decode(encoding='utf-8')
example_str = "Simple is better than complex"
print(example_str.encode(encoding="utf-8"))
encode_str = example_str.encode(encoding="utf-8")
print(encode_str.decode(encoding="utf-8"))