# coding=utf-8
print('###### 字符串 ######')
'''
像姓名,一句话描述这样的的文本
使用单引号、双引号、三个单引号或三个双引号引起来的一些字符
name = 'Tom'
desc = """my name is Tom."""
特殊字符
\ 转义符
\r 回车
\n 换行
\t tab键
\f 换页
'''
print('I\'m Tom.')
# \r回车是输出到同一行,会覆盖\r之前的输出
print('I\'m \r Tom.')
# \n回车是输出到下一行,不会覆盖\n之前的输出
print('I\'m \n Tom.')
print('I\'m \f Tom.')
name = "Alex Li"
name.capitalize() # 首字母大写
name.casefold() # 大写全部变小写,不怎么用
name.center(50,"-") # 输出 '---------------------Alex Li----------------------'
name.count('lex') # 统计 lex出现次数
name.encode() # 将字符串编码成bytes格式
name.endswith("Li") # 判断字符串是否以 Li结尾
"Alex\tLi".expandtabs(10) # 输出'Alex Li', 将\t转换成多长的空格
name.find('A') # 查找A,找到返回其索引, 找不到返回-1
name = "my name is yerban"
print(name.find("n"))
# 3
print(name[name.find("n"):7])
# find()可用于切片
# 从右边开始查找第一个匹配的下标
print(name.rfind("a"))
# 15
format :
>>> msg = "my name is {}, and age is {}"
>>> msg.format("alex",22)
'my name is alex, and age is 22'
>>> msg = "my name is {1}, and age is {0}"
>>> msg.format("alex",22)
'my name is 22, and age is alex'
>>> msg = "my name is {name}, and age is {age}"
>>> msg.format(age=22,name="ale")
'my name is ale, and age is 22'
format_map
>>> msg.format_map({'name':'alex','age':22})
'my name is alex, and age is 22'
msg.index('a') 返回a所在字符串的索引
'9aA'.isalnum() # 是否为阿拉伯数字和字母 True
'9'.isdigit() # 是否整数
name.isnumeric
name.isprintable
name.isspace
name.istitle
name.isupper
b="ddefdsdff_哈哈"
b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True
"|".join(['alex','jack','rain'])
print("|".join(['alex', 'jack', 'rain']))
# alex|jack|rain
print("+".join(['1', '2', '3']))
# 1+2+3
name = "my name is yerban"
print(name.ljust(50, "*"))
# my name is yerban*********************************
print(name.rjust(50, "*"))
# *********************************my name is yerban
print("HL".lower())
# hl
print("hl".upper())
# HL
print("\nhl")
# 本身会有换行
print("\nhl".lstrip())
# 去掉左边换行符
print("hl\n".rstrip())
# 去掉右边换行符
print(" hl\n".strip())
# 去掉两边换行符或空格
maketrans
>>> intab = "aeiou" #这是具有实际字符的字符串。.
>>> outtab = "12345" #这是具有相应映射字符的字符串.
>>> trantab = str.maketrans(intab, outtab) #把实际字符映射为对应字符。
>>>
>>> str = "this is string example....wow!!!"
>>> str.translate(trantab)
'th3s 3s str3ng 2x1mpl2....w4w!!!'
# partition() 方法用来根据指定的分隔符将字符串进行分割。
# 如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
msg = "my name is {name}, and age is {age}"
msg.partition('is')
# 输出 ('my name ', 'is', ' {name}, and age is {age}')
print("alex li, chinese name is lijie".replace("li", "hl", 1)) #替换
# alex hl, chinese name is lijie
name = "my name is yerban"
# 将空格进行切分称列表
print(name.split())
# ['my', 'name', 'is', 'yerban']
# 将"+"进行切分称列表
print("1+2+3".split("+"))
# ['1', '2', '3']
print(name.swapcase()) # 大小写互换
# MY NAME IS YERBAN
print(name.title())
# My Name Is Yerban
>>> name.zfill(40)
# '00000my name is {name}, and age is {age}' 填充0