Python入门基础007—字符串

1.驻留机制:

驻留机制(交互模式):仅保存一份相同且不可变字符串的方法,不同的值被存放在驻留池当中,后续创建相同字符串时不会开辟新空间,而是直接赋地址
 1.字符串的长度为0或者1时
 2.符合标识符的字符串(字母,数字,下划线组成)
 3.[-5,256]之间的整数数字

2.字符串的查询操作:

#index和rindex  不存在时抛出error
#find与rfind  不存在时返回-1
s='hello,hello'
print(s.index('lo'))#3,注意index前面是英文顿号
print(s.rindex('lo'))#rindex:最后一次出现lo的位置
#print(s.index('m')) #value error
print(s.find('lo'))
print(s.rfind('lo'))#rfind:最后一次出现lo的位置
print(s.find('m'))#  -1

 3.字符串的大小写转换:

s='hello,pytHon'
print(s.upper())#转成大写之后会产生新的字符串对象
print(s.lower())#转成小写之后会产生新的字符串对象
print(s.swapcase())#大变小,小变大
print(s.capitalize())#把第一个字符变大写,其余都小写
print(s.title())#把每个单词第一个变成大写,其他全小写
HELLO,PYTHON
hello,python
HELLO,PYThON
Hello,python
Hello,Python

4.字符串的对齐操作:

s='hello,python'
print(s.center(20,'*'))#居中对齐,第一个参数是宽度,第二个参数是填充物
print(s.center(20))#第二个参数没有则默认空格
print(s.center(10,'*'))#宽度小于原参数时直接左对齐全部输出

print(s.ljust(20,'*'))#左对齐
print(s.ljust(10,'*'))
print(s.ljust(20))

print(s.rjust(20,'*'))#右对齐

print(s.zfill(20))#右对齐,使用0填充
print(s.zfill(10))#返回原字符串
print('-8910'.zfill(6))

 

 5.字符串的劈分操作:

#split()与rsplit(向右分割)
s='hello world python'
print(s.split())#默认分割符号为空格
s1='hello|world|python'
print(s1.split())
print(s1.split(sep='|'))#分割符号为|
print(s1.split(sep='|',maxsplit=1))#只分割一次

6.判断字符串的方法:

s1='hello,world'
print(s1.isidentifier())#判断是不是合法的字符串
print('\t'.isspace())#判断字符串是否由空白字符组成
print('abc'.isalpha())#判断字符串是否由字母组成
print('123'.isdecimal())#判断字符串是否由十进制的数字组成
print('123四'.isnumeric())#判断字符串是否由数字组成
print('123abc'.isalnum())#判断字符串是否由字母和数字组成

7. 字符串的替换与合并:

s='hello,python'
print(s.replace('python','java'))
s1='hello,python,python,python'
print(s1.replace('python','java',1))#第一个被替换的字符串,第二个需要替换的字符串,第三个最大替换次数

lst=['hello','java','python']
print('|'.join(lst))
print(''.join(lst))
t=('hello','java','python')
print(''.join(t))
print('*'.join(t))

 8.字符串的比较操作:

print('apple'>'app')#True
print('apple'>'banana')#False
print(ord('a'),ord('b'),ord('杨'))
print(chr(97),chr(98),chr(26472))
#==比较value   is比较id

 

9.字符串的切片操作;

s='hello,world'
s1=s[0:5]
s2=s[6:]
s3=s1+'!'+s2
print(s3)
print(s[::-2])#负数表示从后面倒序
print(s[-5::1])

 

 10.格式化字符串:

#1. % 占位符
name='张三'
age=20
print('我叫%s,今年%d岁' %(name,age))
print('我叫'+name+'今年'+str(age)+'岁')
#2.{}占位符
print('我叫{0},今年{1}岁'.format(name,age))
#3.fstring
print(f'我叫{name},今年{age}岁')

print('%10d' %99)#10表示宽度
print('%.3f' %3.1415926)#.3表示小数点后面三位
print('%10.3f' %3.1415926)#总宽度为10,小数点后面三位

print('{0:.3}'.format(3.141592))#.3表示3位数
print('{:.3f}'.format(3.141592))#.3f表示3位小数
print('{:10.3f}'.format(3.141592))#总宽度为10,小数点后面三位

 

 11.字符的编码与解码:

#编码
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'))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值