其他字符串操作
# 其他字符串操作
# 单引号双引号用法
"""
1、字符串中有单引号,使用双引号
2、字符串中有双引号,使用单引号
3、字符串中有既有单引号又有双引号,使用三引号
"""
str1 = "Let' Go"
print(str1)
str2 = 'My name is "abc"'
print(str2)
str3 = '''My name is "abc". Let' Go'''
print(str3)
# 乘号
"""
1、重复多少次
"""
str4 = 'hello'
print(str4*10)
# 通过索引取值
"""
1、指定索引位置
2、指定索引范围(包括左边,不包括右边)
3、指定索引范围和步进(步进,每隔多少取一次)
"""
str5 = 'hello world'
print(str5[1])
print(str5[1:3])
print(str5[1:7:2])
# 成员运算
"""
1、判断内容是否在字符串中,返回值为True或False
"""
str6 = 'hello'
print('e' in str6)
# join方法
"""
1、将多个字符串组合成一个新的字符串
2、使用指定字符或符号链接字符串
"""
a = '123'
b = '456'
c = '789'
print(''.join([a, b, c]))
print(','.join([a, b, c]))
# count方法
"""
1、统计元素个数
"""
str7 = 'hello'
print(str7.count('l'))
# capitalize
"""
1、将首字母大写
"""
str8 = 'hello'
print(str8.capitalize())
# center
"""
1、居中
2、居中并指定分隔符(默认是空格)
"""
str9 = 'hello'
print(str9.center(80))
print(str9.center(80, '-'))
# encode 详见字符串编码
# endswith
"""
1、判断结尾返回是True或False
2、可以是多个元素
"""
str11 = 'hello'
print(str11.endswith('o'))
print(str11.endswith('lo'))
print(str11.endswith('a'))
# startswith
"""
1、判断开头返回是True或False
2、可以是多个元素
"""
str12 = 'hello'
print(str12.startswith('h'))
print(str12.startswith('he'))
print(str12.startswith('a'))
# expandtabs
"""
1、设置制表符\t有多少空格
"""
str13 = 'hello\tworld'
print(str13.expandtabs(30))
# find
"""
1、查找到第一个指定元素,并返回索引值
"""
str14 = 'hello world'
print(str14.find('o'))
# format
"""
1、详见格式化输出
"""
str15 = 'hello {}'
print(str15.format('world'))
# format_map
"""
1、和format,传入值为字典
"""
str16 = 'hello {a}'
print(str16.format_map({'a': 'world'}))
# index
"""
1、和find类似,但是找不到的话会报错
"""
str17 = 'hello'
print(str17.index('h'))
# isalnum
"""
1、判断是否是数字或字母,返回值为True或False
"""
str18 = 'hello'
print(str18.isalnum())
# isdecimal
"""
1、判断是否是十进制,返回值为True或False
"""
print('10'.isdecimal())
print('1F'.isdecimal())
# isdigit
"""
1、判断字符串是否能转换成整型数字,返回值为True或False
"""
print('10'.isdigit())
print('abc'.isdigit())
print('1.2'.isdigit())
# isidentifier
"""
1、检测字符串是否可以作为变量名,返回值为True或False
"""
print('abc'.isidentifier())
print('123'.isidentifier())
# islower
"""
1、检测是否为全小写,返回值为True或False
"""
print('abc'.islower())
print('aBC'.islower())
# isupper
"""
1、检测是否为全大写,返回值为True或False
"""
print('ABC'.isupper())
print('aBC'.isupper())
# isspace
"""
1、判断是否全是空格,返回值为True或False。
"""
print(' '.isspace())
print('1 2'.isspace())
# istitle
"""
1、是否是标题格式(每一个单词首字母大写),返回值为True或False。
"""
print('My Title'.istitle())
print('My title'.istitle())
# 大小写转换
"""
1、大写转小写
2、小写转大写
3、大小写互换
"""
print('Hello'.lower())
print('Hello'.upper())
print('Hello'.swapcase())
# 靠左靠右
"""
1、靠左
2、靠右
"""
print('hello'.ljust(30, '-'))
print('hello'.rjust(30, '-'))
# strip
"""
1、删除开头结尾的空格换行制表符
"""
print(' abc '.strip())
print(' abc\n'.strip())
# lstrip
"""
1、删除开头的空格换行制表符
"""
print(' abc '.lstrip())
# rstrip
"""
1、删除结尾的空格换行制表符
"""
print(' abc '.rstrip())
# replace
"""
1、查找替换指定内容
"""
print('abc'.replace('a', 'A'))
# split
"""
1、分割字符串,通过指定内容对字符串进行分割,输出列表
"""
print('hello world'.split(' '))
# title
"""
1、将字符串转换成title格式(每个单词首字母大写)
"""
print('hello world'.title())
字符串编码
# 字符串编码
"""
1、ASCII:只能存英文数字和拉丁字符,一个字符占一个字节,8位
2、gb2312: 只有6700多个中文。
3、gbk: 2w多中文
4、gb18030: 2w7中文
5、unicode(万国码,是一个标准并非编码集):
utf-32 任意一个字符占4个字节
uft-16 一个字符占2个字节或两个以上
utf8 一个英文用ASCII来存,占一个字节,一个中文占3个字节
"""
# 编码和解码
"""
1、bytes是一种比特流(数据流),它的存在形式是01010001110这种
2、str->bytes:encode编码
3、bytes->str:decode解码
"""
# 演示1、将字符串转换成bytes类型,在转换成字符类型
s1 = '编码测试'
b = bytes(s1, encoding='utf8')
s2 = b.decode('utf8')
print(s1)
print(b)
print(s2)