字符串的操作、切片、格式化与编码转换

定义:python字符串驻留机制的定义,python仅保存一份相同且不可变换字符串,后续在创建相同字符串时,不开辟新空间,而是把该字符串地址赋给新创建的变量

a = 'python'
b = "python"
print(id(a),id(b))

驻留机制条件:

  • 字符串长度为0 或1
  • 符合标识符的字符串(字母数字下划线)
  • 字符串只在编译时进行驻留,而非运行时
  • [-5,256]之间的整数数字
    pycharm 对字符串进行了优化处理,所以不在上述条件,在pycharm中也会发现其驻留。

驻留机制的优缺点:

  • 避免频繁的在内存中创建和销毁,提升效率节约内存,拼接和修改字符串会影响性能
  • 在需要进行字符串拼接时建议用jion而非“+”,因为join的方法是先计算出所有字符的长度,然后再拷贝,只新建了一次,效率比“+”高

字符串的常用操作
查询办法,index() ,rindex() ,find() ,rfind( )

s='hello,hello'
print(s.index('o'))
print(s.rindex('o'))  #如果不在字符串里,报错
print(s.find('lo'))
print(s.rfind('la'))  #a不在字符串里,返回-1
4
10
3
-1

字符串的大小写转换

  • upper() 所有字符转化为大写
  • lower() 所有字符转化为小写
  • swapcase() 大写转小写,小写转大写
  • capitalize() 第一字母转大写,其余转小写
  • title() 把单词的每一个字符转为大写,剩余字符转为小写
s= 'hello,python'
print(s,id(s))
a= s.upper()
b = s.lower()
print(a,id(a))
print(b,id(b))   #小写化之后,实际未发生变化,但已开辟新内存
s= 'HELLO,python'
a= s.swapcase()  #
print(a)
b= s.capitalize()
print(b)
c=s.title()
print(c)
s= 'hello,python'
print(s,id(s))
a= s.upper()
b = s.lower()
print(a,id(a))
print(b,id(b))   #小写化之后,实际未发生变化,但已开辟新内存
s= 'HELLO,python'
a= s.swapcase()  #大写转小写,小写转大写
print(a)
b= s.capitalize()  #第一字母转大写,其余转小写
print(b)
c=s.title()   #把单词的每一个字符转为大写,剩余字符转为小写
print(c)

字符串内容对其的操作方法

  • center() 居中对齐
  • ljust() 左对齐
  • rjust() 右对齐
  • zfill()
s= '-hello,Python'
a = s.center(21,'*')
print(s)
print(a)
b = s.ljust(20,'=')
print(b)
c = s.ljust(10,'=')  #宽度超出字符串限制后,无意义,返回字符串本身
print(c)
d = s.rjust(20,'=')
print(d)
e = s.zfill(20)  #在-号后和字符前加0
print(e)
-hello,Python
****-hello,Python****
-hello,Python=======
-hello,Python
=======-hello,Python
-0000000hello,Python

字符串的分割
split(sep =‘m’,maxsplit=2) 分割符,最大分割次数

s = 'hellomymymyPython'
a = s.split()
print(a,type(a)) #字符串被空格分割为列表
b= s.split(sep ='m',maxsplit=2)  #使用m分割,最大劈分次数为2
print(b)
b= s.rsplit(sep ='m',maxsplit=2)  #从右侧开始劈分
print(b)
['hellomymymyPython'] <class 'list'>
['hello', 'y', 'ymyPython']
['hellomy', 'y', 'yPython']

字符串的判断

print('1hello'.isidentifier())  #首字母1,不是标准字符串
print('张三'.isidentifier())   #张三是标砖字符串
print('\t'.isspace())
print('\n'.isspace())
print('1张三'.isalpha())  #张三不是字母
print('0120m'.isdecimal())  #m不是十进制数字
print('314四'.isnumeric())  #四也被认为是数字
print('Ⅳ'.isnumeric())   #罗马数字被认为是数字
print('123qwe'.isalnum()) #全部由数字和字母组成
print('123qwe!'.isalnum()) #!不是数字和字母组成,返false

字符串的拼接

s='hello Python Python Python'
print(s.replace('Python','Java'))
print(s.replace('Python','Java',2))  #替换2次
s1=['hello','hello','hello']
print('_'.join(s))    #字符串之间连接
print('_'.join(s1))   #列表之间连接
t=('hello','python','java')
print('!'.join(t))   #元组之间连接
print('8'.join('hello')) #用8将括号内的字符串分隔开
hello Java Java Java
hello Java Java Python
h_e_l_l_o_ _P_y_t_h_o_n_ _P_y_t_h_o_n_ _P_y_t_h_o_n
hello_hello_hello
hello!python!java
h8e8l8l8o

字符串的比较操作

print(ord('e'),ord('a'))  #ord与chr为相反操作
print(chr(101),chr(97))  #asic码值
print('apple'>'appla')  返回true

字符串的切片操作
字符串是不可变型,切片后将产生新的对象

s= 'hello,python'
s1= s[:2]  #占用新内存
s2= s[4:]  #占用新内存
s3 = '!'   #占用新内存
print(s1+s3+s2)
s4=s[::-2]  #切片步长设置-2
print(s4+s3)
print(s[-6::1])  #索引从-6开始
he!o,python
nhy,le!
python

格式化字符串
格式化字符串就是按一定格式输出的字符串。

  • %s %i或%d %f 分别表示:字符串、整数、浮点数
  • 使用{ } 和.format语句
  • f-string
name='lili'
age = 28
print('我的名字叫%s,今年%d岁' %(name,age))
print('我叫{0},今年{1}岁'.format(name,age))
print(f'我叫{name},今年{age}岁')
print('%10d' %age) #28的宽度为10
i=3.1517826
print('%7.3f'%i)   #总宽度为7,四舍五入保留小数点后3位
print('{0:.4}'.format(i))   #使用花括号时一共占4位数
print('{0:10.4f}'.format(i))   #宽度为10,小数点后有四位
我的名字叫lili,今年28岁
我叫lili,今年28岁
我叫lili,今年2828
  3.152
3.152
    3.1518

字符串的编码转换

s='床前明月光'
byte1 = s.encode(encoding='GBK')
print(byte1.decode(encoding='GBK'))
byte2 = s.encode(encoding='utf-8')  #同样的编码方式同样的解码方式来解
print(byte2.decode(encoding='utf-8'))
床前明月光
床前明月光
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值