# -*-coding:utf-8 -*-
# @Time : 15:56
# @Author: 黄荣津
# @File : 26.字符串的创建与驻留机制.py
# @Software: PyCharm
#字符串的机制
a='python'
b="python"
c='''python'''
print(a,id(a))
print(b,id(b))
print(c,id(c))
# -*-coding:utf-8 -*-
# @Time : 16:13
# @Author: 黄荣津
# @File : 27.字符串的查询操作.py
# @Software: PyCharm
#字符串的查询操作
s='hello,hello'
print(s.index('lo')) #3
print(s.find('lo')) #3
print(s.rindex('lo')) #9
print(s.rfind('lo')) #9
# print(s.index('k')) ValueError: substring not found
print(s.find('k')) #-1
# print(s.rindex('k')) ValueError: substring not found
print(s.rfind('k')) #-1
# -*-coding:utf-8 -*-
# @Time : 19:30
# @Author: 黄荣津
# @File : 28.字符串的大小写转换操作方法.py
# @Software: PyCharm
s='hello python'
a=s.upper() #转成大写之后,会产生一个新的字符串对象
print(a,id(a))
print(s,id(s))
# HELLO PYTHON 2115686087536
# hello python 2115686087664
print(s.lower(),id(s.lower())) #转成之后,会产生一个新的字符串对象
print(s,id(s))
# hello python 1982139693808
# hello python 1982139382768
s2='hello Pthon'
print(s2.swapcase()) #大小写互换
print(s2.title()) #把每一个单词的第一个字符转换成大写,把每一个单词的剩余字符转换为小写
print(s2.capitalize()) #把第一个字符转换为大写,把其余的字符转换为小写
#
# HELLO pTHON
# Hello Pthon
# Hello pthon
# -*-coding:utf-8 -*-
# @Time : 19:42
# @Author: 黄荣津
# @File : 29.字符串内容对齐操作的方法.py
# @Software: PyCharm
s='hello,python'
'''居中对齐'''
print(s.center(20,'*'))
# ****hello,python****
'''左对齐'''
print(s.ljust(20,'*'))
print(s.ljust(10,'*'))
print(s.ljust(10))
# hello,python********
# hello,python
# hello,python
'''右对齐'''
print(s.rjust(20,'*'))
print(s.rjust(10,'*'))
print(s.rjust(10))
# ********hello,python
# hello,python
# hello,python
'''右对齐,使用0进行填充'''
print(s.zfill(20))
print(s.zfill(10))
# 00000000hello,python
# hello,python
# -*-coding:utf-8 -*-
# @Time : 19:53
# @Author: 黄荣津
# @File : 30.字符串的劈分.py
# @Software: PyCharm
s='hello,python'
lst=s.split()
print(lst)
s1='hello|world|python'
print(s1.split(sep='|'))
print(s1.split(sep='|',maxsplit=1))
# ['hello,python']
# ['hello', 'world', 'python']
# ['hello', 'world|python']
'''rsplit()从右侧开始劈分'''
print(s.rsplit())
print(s1.rsplit('|'))
print(s1.rsplit(sep='|',maxsplit=1))
#
# ['hello,python']
# ['hello', 'world', 'python']
# ['hello|world', 'python']
# -*-coding:utf-8 -*-
# @Time : 20:00
# @Author: 黄荣津
# @File : 31.字符串判断的相关方法.py
# @Software: PyCharm
'''判断指定的字符串是不是合法的标识符'''
s='hello,python'
print(s.isidentifier()) #False
print('hello'.isidentifier()) #True
print('hh_11'.isidentifier()) #True
'''判断是否都由字母组成'''
print('abc'.isalpha()) #True
print('战士'.isalpha()) #True
print('ha1'.isalpha()) #False
'''判断都是由十进制组成'''
print('123'.isdecimal()) #True
print('123的'.isdecimal()) #False
'''判断是否全为数字组成'''
print('123'.isnumeric()) #True
print('123九'.isnumeric()) #True
'''判断是否全为字母和数字组成'''
print('123a'.isalnum()) #True
print('aaa'.isalnum()) #True
print('张三123'.isalnum()) #True
print('123!'.isnumeric()) #True
# -*-coding:utf-8 -*-
# @Time : 20:19
# @Author: 黄荣津
# @File : 32.字符串的替换与合并.py
# @Software: PyCharm
'''字符串的替换replace(),字符串的合并join()'''
s='hello,python'
print(s.replace('python','java'))
#hello,java
s1='hello,python,python,python'
print(s1.replace('python','java',2))
#hello,java,java,python
'''将列表和元组中的字符串合并成一个字符串'''
lst=['hello','java','python ']
print('|'.join(lst))
#hello|java|python
lst=('hello','java','python ')
print(''.join(lst))
#hellojavapython
print('*'.join('python'))
#p*y*t*h*o*n
# -*-coding:utf-8 -*-
# @Time : 20:45
# @Author: 黄荣津
# @File : 33.字符串的比较操作.py
# @Software: PyCharm
print('apple'>'app') #True
print('apple'>'banana') #False,相当于97>98
print(ord('a'),ord('b'))
print(chr(97),chr(98))
'''==与is区别'''
'''
==比较的是value
is比较的是id是否相等
'''
a=b='python'
c='python'
print(a==b) #True
print(b==c) #True
print(a is b) #True
print(b is c) #True
# -*-coding:utf-8 -*-
# @Time : 20:55
# @Author: 黄荣津
# @File : 34.字符串的切片操作.py
# @Software: PyCharm
s='hello,python'
s1=s[:5] #由于没有指定起初位置,所以从开始切
s2=s[6:] #由于没有指定结束位置,所以切到字符串的最后一个元素
print(s1)
print(s2)
s3='!'
newstr=s1+s3+s2
print(newstr)
#hello!python
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr))
# 2271229230192
# 2271229277168
# 2271229531760
# 2271229230000
# 2271229541232
print('--------切片[start:end:step]---------')
print(s[1:5:1]) #从1开始截到5(不包括5),步长为1
# -*-coding:utf-8 -*-
# @Time : 21:20
# @Author: 黄荣津
# @File : 35.格式化字符串.py
# @Software: PyCharm
'''格式化字符串'''
#1.%占位符
name='张三'
age=20
print('我叫%s,今年%d岁'%(name,age))
#2.{}作占位符
print('我叫{0},今年{1}岁'.format(name,age))
#3.f-string
print(f'我叫{name},今年{age}岁')
print('%10d'%99) #10表示的是宽度
print('%.3f' %3.1415926) #.3保留3位小数
#同时表示宽度和精度
print('%10.3f'%3.141596) #总宽度位10,小数点后3位
print('{:.3}'.format(3.1415926)) #.3表示的是一共是3位数
print('{:.3f}'.format(3.1415926)) #.3f表示的是3位小数
print('{:10.3f}'.format(3.1415926)) #.3f表示的是3位小数,宽度一共是10位
# -*-coding:utf-8 -*-
# @Time : 21:36
# @Author: 黄荣津
# @File : 36.字符串的编码与解码.py
# @Software: PyCharm
s='天涯共此时'
#编码
print(s.encode(encoding='GBK')) #在GBK这种编码格中,一个中文占两个字节
print(s.encode(encoding='UTF-8')) #在UTF-8这种编码格中,一个中文占三个字节
#解码
#byte代表就是一个二进制数据(字节类型的数据)
byte=s.encode(encoding='GBK') #编码
print(byte.decode(encoding='GBK')) #解码
byte=s.encode(encoding='UTF-8') #编码
print(byte.decode(encoding='UTF-8')) #解码