'''字符串的驻留机制'''
a='python'
b="python"
c='''python'''
print(a,id(a))
print(b,id(b))
print(c,id(c))
a1='abc%'
a2='abc%'
print(a1 is a2)
s='hello,hello'
print(s.index('h'))
print(s.rindex('h'))
print(s.find('lo'))
print(s.rfind('lo'))
print(s.find('k'))
print(s.rfind('k'))
'''字符串中的大小写转换方法'''
s='hello,python'
a=s.upper()
print(a,id(a))
print(s,id(s))
b=s.lower()
print(b,id(b))
print(b==s)
print(b is s)
s2='hello,python'
print(s2.swapcase())
print(s2.title())
'''字符串对齐'''
s='hello,Python'
'''居中对齐'''
print(s.center(20, '*'))
'''左对齐'''
print(s.ljust(20,'*'))
print(s.ljust(10,'*'))
print(s.ljust(20))
'''右对齐'''
print(s.rjust(20,'*'))
print(s.rjust(10,'*'))
print(s.rjust(20))
'''右对齐,使用0进行填充'''
print(s.zfill(20))
print(s.zfill(10))
'''字符串的分割'''
s='hello world Python'
lst=s.split()
print(lst)
s1='hello|world|python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
print('-------')
print(s.rsplit())
print(s1.rsplit(sep='|',maxsplit=1))
'''字符串判断的方法'''
s='hello,python'
print('1:',s.isidentifier())
print('2','hello'.isidentifier())
print('3','张三'.isidentifier())
print('4','张三_123'.isidentifier())
print('5','\t'.isspace())
print('6','abc'.isalpha())
print('7','张三'.isalpha())
print('8','张三1'.isalpha())
print('9','123'.isdecimal())
print('10','123四'.isdecimal())
print('11','ⅠⅡⅢ'.isdecimal())
print('12','123'.isnumeric())
print('13','123四'.isnumeric())
print('14','ⅠⅡⅢ'.isnumeric())
print('15','abc1'.isalnum())
print('16','张三123'.isalnum())
print('17','abc!'.isalnum())
'''字符串的替换'''
s='hello,python'
print(s.replace('python','java'))
s1='hello,python,python'
print(s1.replace('python','java',2))
lst=['hello','python','java']
print('|'.join(lst))
print(''.join(lst))
t={'hello','java','python'}
print(' '.join(t))
print('*'.join('python'))
print('apple'>'app')
print('apple'>'banana')
print(ord('a'),ord('b'))
print(ord('李'))
print(chr(81),chr(98))
print(chr(26446))
'''==与is的区别
== 比较的是value是否相等
is 比较的是id是否相等
'''
a=b='python'
c='python'
print(a==b)
print(b==c)
print(a is b)
print(a is c)
print(b is c)
'''字符串的切片操作将产生新的字符串'''
s='hello,python'
s1=s[:5]
print(s1)
s2=s[6:]
print(s2)
s3='!'
new_s=s1+s3+s2
print(new_s)
print('------------')
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(new_s))
print('-----------------切片[start:end:step]---------------------')
print(s[1:5:1])
print(s[::2])
print(s[::-1])
print(s[-6::])
'''格式化字符串'''
name='张三'
age=12
print('我家%s,今年%d岁' % (name , age))
print('我叫{0},今年{1}'.format(name,age))
print(f'我叫{name},今年{age}')
'''精度和宽度'''
print('%10d' % 99)
print('%.3f' % 3.1415926)
print('%10.3f' % 3.1415926)
print('hellohello')
print('{0}'.format(3.1415926))
print('{0:.3}'.format(3.1415926))
print('{0:.3f}'.format(3.1415926))
print('{0:10.3f}'.format(3.1415926))
'''字符串的编码与解码'''
s='天涯共此时'
print(s.encode(encoding='GBK'))
print(s.encode(encoding='UTF-8'))
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
byte=s.encode(encoding='UTF-8')
print(byte.decode(encoding='UTF-8'))