# str类型
a = "SEPIA" # a = str("SEPIA")
# 类型转换
# str可以把任意其他类型转换成字符串
res = str({"a": 1})
res1 = {"a": 1}
print(res, type(res))
print(res1, type(res1))
# 使用
# (1).按索引取值(正向取,反向取):只能取
msg = "Hello world"
# 正向取
print(msg[0])
print(msg[4])
# 反向取
print(msg[-1])
# (2).切片
res = msg[0:5]
print(res)
# (3).步长
res = msg[0:5:2] # 从第0位开始取到第4位,间隔为2位
print(res)
res = msg[:] # 正向取全部
# .反向步长
res = msg[5:0:-1] # 从第5位开始取到第1位,间隔为1位
print(res)
res = msg[::-1] # 反向取全部
# .取长度
msg = "Hello world"
print(len(msg))
# .判断字符串是否在一个大字符串中
x = "e"
# print("e" in msg)
print(x in msg)
# .移除空白strip
msg = " 111 222 "
res = msg.strip() # 默认去掉左右两侧的空格
print(msg)
print(res)
msg = "******111 222******"
res = msg.strip("*") # 去掉左右两侧的*
print(msg)
print(res)
# .切分split
# 把一个字符串按照某种分隔符进行切分,得到一个列表
info = "SEPIA 32 male"
res = info.split() # 默认按照空格分割
print(res)
info = "SEPIA:32:male"
res = info.split(":", 1) # 默认按照":"分割, 指定分割次数
print(res)
# ————————————————————————————
# 需要掌握的操作
# strip,lstrip,rstrip
msg = "****111 222****"
print(msg.strip("*")) # 去掉两侧的"*"
print(msg.lstrip("*")) # 去掉左边的"*"
print(msg.rstrip("*")) # 去掉右边的"*"
# lower,upper
msg = "HELLO world"
print(msg.lower()) # 转换成小写
print(msg.upper()) # 转换成大写
# startswith,endswith
print("Hello world".startswith("Hello")) # 判断是否以""为开头
print("Hello world".endswith("world")) # 判断是否以""为结尾
# split,rsplit
info = "SEPIA:32:male"
res = info.split(":", 1) # 从左到右分割, 指定分割次数
res1 = info.rsplit(":", 1) # 从右到左分割, 指定分割次数
print(res)
print(res1)
# join
l1 = ["SEPIA", "32", "male"]
res = " ".join(l1)
print(res)
# replace
msg = "you can you up, no can no bb"
res = msg.replace("can", "CAN", 1) # 替换前字符串,替换后字符串,替换次数
print(res)
# isdigit
# 判断是否为纯数字
age = "32"
print(age.isdigit())
print(int(age), type(int(age)))
# ————————————————————————————
# 需要了解的操作
# find
msg = "hello world"
res = msg.find("e") # 找到所查找字符的位置,找不到时返回-1
print(res)
# index
msg = "hello world"
res = msg.index("e") # 找到所查找字符的位置,找不到时报错
print(res)
# count
msg = "hello world"
res = msg.count("o") # 查找出现的次数
print(res)
# center,ljust,rjust,zfill
print("SEPIA".center(20, "*")) # 打印时居中并用*填充空白
print("SEPIA".ljust(20, "*")) # 打印时左对齐并用*填充空白
print("SEPIA".rjust(20, "*")) # 打印时右对齐并用*填充空白
print("SEPIA".zfill(20)) # 打印时右对齐并用0填充空白
# expandtabs
msg = "hello\tworld"
print(msg.expandtabs(2)) # 指定制表符为2
# capitalize, swapcase, title
print("hello world".capitalize()) # 首字母大写
print("HELLO world".swapcase()) # 大小写反转
print("HELLO world".title()) # 每个单词首字母大写
# is
num1 = b"4"
num2 = u"4"
num3 = "四"
num4 = "Ⅳ"
# isdigit识别数字
print(num1.isdigit()) # true
print(num2.isdigit()) # true
print(num3.isdigit()) # false
print(num4.isdigit()) # false
# isnumeric识别数字,只能识别num2,num3,num4
print(num2.isnumeric()) # true
print(num3.isnumeric()) # true
print(num4.isnumeric()) # true
# isdecimal识别数字,只能识别num2
print(num2.isdecimal()) # true
print(num3.isdecimal()) # false
print(num4.isdecimal()) # false
# -----------------------------------------
# is了解
# print("abc".islower()) # 判断小写
# print("ABC".isupper()) # 判断大写
# print("Abc Def".istitle()) # 判断首字母大写
# print("123".isalnum()) # 判断数字
# print("abc".isalpha()) # 判断字母
# print(" ".isspace()) # 判断空格
# print("abc".isidentifier()) # 判断名字是否合法