string


python学习在字符串部分的学习代码:

# 编写人:刘钰琢
# 编写日期:2021/1/20 19:08
#字符串的驻留机制
a='python'
b="python"
c='''python'''
print(a,id(a))#python
print(b,id(b))#python
print(c,id(c))#python
a='abc%'
b="abc%"
print(a==	b)#true
#字符串的查询操作
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'))#会报错
#print(s.rindex('k'))#会报错
print(s.find('k'))#-1,如果没有find就会返回-1
print(s.rfind('k'))#-1
#字符串的大小写转换的方法
print(s.upper())#HELLO,HELLO,全部转化为大写
print(s.lower())#hello,hello 全部转化为小写
print(s.swapcase())#HELLO,hELLO  把字符串所有的字符大写转大写 大写转小写
print(s.capitalize())#Hello,hello对第一个字符大写转小写 ,其余全部转化为小写
print(s.title())#Hello,Hello 对每一个单词的第一个字符转化为大写其他转化为小写
#字符串的各种对齐
s='hello,python'
print(s.center(20,'*'))#****hello,python****居中对齐
print(s.ljust(20,'*'))#hello,python******** 左对齐
print(s.ljust(10,'*'))#hello,python 如果宽度小于字符串长度则,返回元字符
print(s.ljust(20))#hello,python 如果不要填空符则直接填入空格
print(s.rjust(20,'*'))#********hello,python 右对齐
print(s.rjust(10))#hello,python 如果宽度小于字符串长度则,返回元字符
print(s.zfill(20))#00000000hello,python 右对齐,会用0填充
print('-8910'.zfill(8))#-0008910
#字符串的劈分
#split 从左边开始劈分
s='hello world python'
lst=s.split()
print(lst)#['hello', 'world', 'python']
s1='hello|world|python'
print(s1.split(sep='|'))#['hello', 'world', 'python']
print(s1.split(sep='|',maxsplit=1))#['hello', 'world|python'] maxsplit可以限制划分几个部分
#rsplit从右边开始劈分
print(s1.rsplit(sep='|'))#['hello', 'world', 'python']
print(s1.rsplit(sep='|',maxsplit=1))#['hello|world', 'python']
#字符串判断的相关方法
s2='hello,python'
print('1',s.isidentifier())#False 判断字符串是否是合法字符串
print('2','hello'.isidentifier())#True
print('3','张三_'.isidentifier())#True
print('4','张三_123'.isidentifier())#True
print('5','\t'.isspace())#True 判断字符串是否全部都是有空字符组成
print('6','acasdc'.isalpha())#True 判断字符串是否全部都是有字母组成
print('7','张三'.isalpha())#True
print('8','123'.isdecimal())#True 判断字符串是否全部都是有十进制数字 罗马数字,汉子都不是
print('9','123四'.isnumeric())#True 判断字符串是否全部都是有数字组成 罗马数字还有汉字都是
print('10','abc1'.isalnum())#True 判断字符串是否全部都是有字母和s数字组成
print('11','张三123'.isalnum())#True
print('12','abc!'.isalnum())#False !不是字母也不是数字
#字符串的替换和合并
s3='hello,python'
print(s3.replace('python','java'))#hello,java
s4='hello,python,python,python'
print(s4.replace('python','java',2))#hello,java,java,python
lst=['hello','python','world']
print('|'.join(lst))#hello|python|world
print(''.join(lst))#hellopythonworld
print('*'.join('python'))#p*y*t*h*o*n
#字符串的比较操作
#可以使用比较运算符 > >= < <= == !=
print('apple'>'app')#True
print('apple'>'banana')#False
print(ord('a'),ord('b'))#97 98 ord函数可以输出ascall码
print(chr(97),chr(98))#chr 可以和获取ascll所对应的字母或者数字
print(ord('刘'))
print(chr(21016))
# is 是比较id是否相同
#字符串的切片操作
#字符串不具备增删改查操作 切片操作会产生新的对象
s='hello,python'
s1=s[:5]#由于没有指定起始位置,所以从索引为0开始
s2=s[6:]#由于没有指定结束位置,所以会结束到最后
s3='!'
news=s1+s3+s2
print(news)
print(s[1:5:1])#从一开始到结束限制的前一位结束,步长为一
print(s[::2])#可以没有开始也没有结束,但是又步长
print(s[::-1])#从后开始,步长为一
print(s[:-6:-1])
#格式化字符串
#(1) %
name='张三'
age=20
print('我叫%s,今年%d'%(name,age))
#(2) {}
print('我叫{0},今年{1}'.format(name,age))
#3(3) f-string
print(f'wojaio{name},jinnian{age}')
print("%10d"%age)
print('hhhhhhhhhh')
print("%10.3f"%3.1415926)#.3小数点后三位,总宽度为10
print('{0:.3}'.format(3.1415926))#.3表示一共有三个数字
print('{0:10.3f}'.format(3.1415926)) #10是宽度,.3f是小数点
#字符串的编码转换 编码要和解码的格式要相同
#编码
s='天涯共此时'
print(s.encode(encoding='GBK'))#在GBK编码格式中,一个中文占两个字节
print(s.encode(encoding='UTF-8'))#在UTF-8编码格式中,一个中文占三个字节
#解码
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
byte=s.encode(encoding='UTF-8')
print(byte.decode(encoding='UTF-8'))
#结束与 2021/1/22 19:18

代码的运行结果:

python 1961584955248
python 1961584955248
python 1961584955248
True
3
3
9
9
-1
-1
HELLO,HELLO
hello,hello
HELLO,heLLO
Hello,hello
Hello,Hello
****hello,python****
hello,python********
hello,python
hello,python        
********hello,python
hello,python
00000000hello,python
-0008910
['hello', 'world', 'python']
['hello', 'world', 'python']
['hello', 'world|python']
['hello', 'world', 'python']
['hello|world', 'python']
1 False
2 True
3 True
4 True
5 True	
6 True
7 True
8 True
9 True
10 True
11 True
12 False
hello,java
hello,java,java,python
hello|python|world
hellopythonworld
p*y*t*h*o*n
True
False
97 98
a b
21016
刘
hello!python
ello
hlopto
nohtyp,olleh
nohty
我叫张三,今年20
我叫张三,今年20
wojaio张三,jinnian20
		20
hhhhhhhhhh
		3.142	
3.14
		3.142
b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'
天涯共此时
天涯共此时

学习心得: 日后在补充


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}