"""
字符串类型:
一、string类型
    1、作用:
    2、定义:msg = 'hello'  python 底层 msg = str('hello')
    3、类型转换:
        str()可以将任意类型转换为字符串
    4、使用:
        4.1 内置方法-优先掌握
            4.1.1 按索引取值:正向取 msg[1],反向取 msg[-1]
            4.1.2 切片:索引的拓展(顾头不顾尾)
            4.1.3 len 统计字符个数
            4.1.4 in / not in 成员运算
            4.1.5 strip() / lsrtip() / rstrip() 移除空白: 空格、tab(\t)、换行(\n)
            4.1.6 split 切分,把规则字符串按特定字符分割为列表
            4.1.7 循环取值
        4.2 需要掌握
            4.2.1 lower() / upper()
            4.2.2 startswith() / endswith()
            4.2.3 format的三种玩法
            4.2.4 split() / rsplit() 从左边或右边开始切两次,剩余为一个字符串
            4.2.5 join() 指定分隔符将元素为纯字符串的列表拼接为字符串
            4.2.6 replace()
            4.2.7 isdigit()
        4.3 需要了解
            4.3.1 find, rfind, index, rindex, count 查找和统计
            4.3.2 center, ljust, rjust, zfill 设置对齐方式,用特定字符填充空位
            4.3.4 expandtabs 修改\t制表符代表的空格数, n-1 if n>1 else n (n为零或正整数)
            4.3.5 captalize, swapcase, title  英文字母大小写转换
            4.3.6 is数字系列,最常用的是isdigit,可以判断bytes和unicode类型,如果要判断中文数字或罗马数字,则需要用到isnumeric。
            4.3.7 is其他
"""
# 4.1.1 按索引取值
msg = 'hello world'
print(msg[1])   # e
print(msg[-1])  # d
# msg[1] = 'H'    # TypeError: 'str' object does not support item assignment

# 4.1.2 切片:索引的拓展(顾头不顾尾)
print(msg[0:5])         # 0 1 2 3 4 hello
print(msg[0:5:2])       # 0 2 4 hlo
print(msg[4:0:-1])       # 4 3 2 1 olle
print(msg[::-1])        # dlrow olleh 字符串逆序

# 4.1.3 len 统计字符个数

# 4.1.4 in / not in 成员运算
print('he' in msg)      # True
print('he' not in msg)  # False

# 4.1.5 strip() / lsrtip() / rstrip() 移除空白(空格、tab(\t)、换行(\n))
msg = ' hello world\t'
print(msg, len(msg))                    # ' hello world     13'
print(msg.strip(), len(msg.strip()))    # 'hello world 11'
print(msg.lstrip(), len(msg.lstrip()))  # 'hello world      12'
print(msg.rstrip(), len(msg.rstrip()))  # ' hello world 12'
msg = '**/*=-hello*/-=-/>;'
print(msg.strip('*/=->;'))      # hello,参数为需要去除的字符

# 4.1.6 split 切分,把规则字符串按特定字符分割为列表
# 第一个参数为分隔符,默认为空格;第二个参数指定分割次数,默认全部分割
info = 'tom,18,3.1K,male'
print(info.split(','))      # ['tom', '18', '3.1K', 'male']

# 4.1.7 循环取值
for s in info:
    print(s)

# 4.2.1 lower() / upper()
msg = 'Abc'
print(msg.lower(), msg.upper())     # abc ABC

# 4.2.2 startswith() / endswith()
print(msg.startswith('A'), msg.endswith('C'))   # True False

# 4.2.3 format的三种玩法

# 4.2.4 split() / rsplit()
info = 'tom,18,3.1K,male'
print(info.split(',', 2))      # ['tom', '18', '3.1K,male'] 从左边开始切两次,剩余为一个字符串
print(info.rsplit(',', 2))      # ['tom,18', '3.1K', 'male']从右边开始切两次,剩余为一个字符串

# 4.2.5 join() 指定分隔符将元素为纯字符串的列表拼接为字符串
lst = ['tom', '18', '3.1K', 'male']
print(':'.join(lst))    # 'tom:18:3.1K:male'

# 4.2.6 replace()
msg = 'AAAAbc'
print(msg.replace('A', 'a', 2)) # aaAAbc
# 4.2.7 isdigit() 判断字符串是否由纯数字组成
print('123'.isdigit())      # True
print('123.23'.isdigit())   # False

# 4.3.1 find, rfind, index, rindex, count 查找和统计
msg = 'AAAAbc'
print(msg.find('A'))    # 0 找到符合条件的第一个索引,找不到返回-1
print(msg.find('b', 4, 6))    # 4 找到符合条件的第一个索引,找不到返回-1,可以指定查找范围
print(msg.find('d', 4, 6))    # -1 找到符合条件的第一个索引,找不到返回-1,可以指定查找范围
print(msg.rfind('A'))   # 3 自右向左找到符合条件的第一个索引,找不到返回-1

print(msg.index('A'))   # 0 能找到视同find
# print(msg.index('d'))   # ValueError: substring not found  找不到报错
print(msg.rindex('A'))   # 3 自右向左找到符合条件的第一个索引,找不到报错

print(msg.count('A'))   # 4 统计字符串在大字符串中出现的次数
print(msg.count('A', 1,3))   # 2 统计字符串在大字符串中出现的次数,可以指定统计范围

# 4.3.2 center, ljust, rjust, zfill 设置对齐方式,用特定字符填充空位
print(msg.center(30, '-'))  # ------------AAAAbc------------
print(msg.ljust(30, '-'))   # AAAAbc------------------------
print(msg.rjust(30, '*'))   # ************************AAAAbc
print(msg.zfill(30))        # 000000000000000000000000AAAAbc    用零(zero)填充

# 4.3.4 expandtabs # 修改\t制表符代表的空格数, n-1 if n>1 else n (n为零或正整数)
msg = 'a\tBc'
print(len(msg))
print(msg)
print(msg.expandtabs(3))    # a  Bc(2个空格)
for i in range(11):
    print(len(msg.expandtabs(i))-3, end=',')   # 0,1,1,2,3,4,5,6,7,8,9,
print()

# 4.3.5 captalize, swapcase, title 英文字母大小写转换
msg = 'aBe heLLo'
print(msg.capitalize()) # Abe hello 首字母大写,其他字母小写
print(msg.swapcase())   # AbE HEllO 大小写反转
print(msg.title())      # Abe Hello 每个单词首字母大写,其他字母小写

# 4.3.6 is数字系列,最常用的是isdigit,可以判断bytes和unicode类型,如果要判断中文数字或罗马数字,则需要用到isnumeric。
print(b'4', b'4'.isdigit())   # True bytes类型整数
# print(b'4', b'4'.isdecimal()) # bytes类型无此方法
# print(b'4', b'4'.isnumeric()) # bytes类型无此方法

print('4', '4'.isdigit())   # True 正整数字符串
print('4', '4'.isdecimal()) # True 正整数字符串
print('4', '4'.isnumeric()) # True 正整数字符串

print('四', '四'.isdigit())   # False 中文数字
print('四', '四'.isdecimal()) # False 中文数字
print('四', '四'.isnumeric()) # True 中文数字

print('肆', '肆'.isdigit())   # False 中文大写数字
print('拾', '拾'.isdecimal()) # False 中文大写数字
print('陆', '陆'.isnumeric()) # True 中文大写数字

print('Ⅳ', 'Ⅳ'.isdigit())   # False 罗马数字,不是字母IV
print('Ⅳ', 'Ⅳ'.isdecimal()) # False 罗马数字,不是字母IV
print('Ⅳ', 'Ⅳ'.isnumeric()) # True 罗马数字,不是字母IV

print('4.3', '4.3'.isdigit())   # False 浮点数
print('4.3', '4.3'.isdecimal()) # False 浮点数
print('4.3', '4.3'.isnumeric()) # False 浮点数

print('-4', '-4'.isdigit())   # False 负整数
print('-4', '-4'.isdecimal()) # False 负整数
print('-4', '-4'.isnumeric()) # False 负整数


# 4.3.7 is其他
print('abc123'.isalnum())       # True 字符串中既可以包含数字也可以包含字母
print('abc123'.isalpha())   # False 字符串中只包含字母
print('abc123'.isidentifier())  # True 是否是识别字(关键字+变量名格式)
print('abc123'.islower())   # False 字符串是否是纯小写字母
print('abc123'.isupper())   # False 字符串是否是纯大写字母
print('abc123'.isspace())   # False 字符串是否全是空格
print('abc123'.istitle())   # False 字符串中的单词首字母是否都是大写