python 字符串

  • 字符串驻留机制(地址)
  • 查询 推荐.find() .rfind()
  • 大小写转换 .upper() .lower() .swapcase() .capitalize() .title()
  • 内容对齐 .center() .ljust() .rjust() .zfill()
  • 劈分 .split() .rsplit()
  • 字符串内容判断 .isidentifier() .isspace() .isalpha() .isdecimal() .isnumeric() .isalnum()
  • 替换 .replace()
  • 合并 .join()
  • 比较 >< ord(字符) chr(ascii码值)
  • 切片 [start,end,step]
  • 格式化字符串 %占位符, {}占位符
  • 字符编码 .encode() .encode()
# ==================== 字符串的驻留机制
a = 'python'
b = "python"
c = '''python'''
print(id(a))
print(id(b))
print(id(c)) # 地址相同!
# 驻留:仅保存一份相同且不可变字符串,只把相同字符串的地址赋给新建变量
# 交互式演示驻留机制(控制台)参考:https://www.bilibili.com/video/BV1wD4y1o7AS?p=76

# ==================== 字符串的常用操作
# 查询 .index() .rindex() .find() .rfind() 推荐使用后两个
s = 'hello,hello'
print(s.index('lo'))  # 3 if能找到,return子串第一次出现的位置
print(s.find('lo'))   # 3 同上
print(s.rindex('lo')) # 9 if能找到,return子串最后一次出现的位置
print(s.rfind('lo'))  # 9 同上
# print(s.index('k'))   # ValueError if找不到,则报错
print(s.find('k'))      # -1 if找不到,return -1
# print(s.rindex('k'))  # ValueError if找不到,则报错
print(s.rfind('k'))     # -1 if找不到,return -1

# 大小写转换 .upper() .lower() .swapcase() .capitalize() .title()
s = 'Hello,World'
print(id(s))   # 9719088
a = s.upper()  # 全转大写
print(a,id(a)) # HELLO,WORLD 7265904 相当于新串
b = s.lower()  # 全转小写
print(b)       # hello,world
c = s.swapcase() # 大转小,小转大
print(c)         # hELLO,wORLD
d = s.capitalize()  # 仅首字母大写
print(d)            # Hello,world
e = s.title()    # 每个单词首字母大写
print(e)         # Hello,World

# 内容对齐 .center() .ljust() .rjust() .zfill()
s = 'hello,Python'
print(s.center(20,'*')) # ****hello,Python**** 居中对齐
print(s.ljust(20,'^'))  # hello,Python^^^^^^^^ 左对齐
print(s.rjust(20))      #         hello,Python 右对齐,默认符号为空格
print(s.zfill(20))      # 00000000hello,Python 右对齐,左侧用0填充
print(s.ljust(5))       # hello,Python 长度小于原字符,则返回原字符
print('-233'.zfill(8))  # -0000233

# 劈分(分段) .split() .rsplit()
s = 'hello world Python'
s1 = 'hello|world|Python'
# .split()默认分隔符为空格,参数:sep指定分隔符,maxsplit指定劈分次数 .rsplit()从右开始识别劈分
print(s.split())              # ['hello', 'world', 'Python']
print(s1.split(sep='|'))      # ['hello', 'world', 'Python']
print(s1.split(sep='|',maxsplit=1))   # ['hello', 'world|Python']
print(s.rsplit(maxsplit=1))   # ['hello world', 'Python']

# 判断字符串内容 .isidentifier() .isspace() .isalpha() .isdecimal() .isnumeric() .isalnum()
# 1  .isidentifier() 合法标识符:字母数字下划线
# 2  .isspace() 空白字符:回车 换行 水平制表符
# 3  .isalpha() 全部是字母(英文 中文)组成
# 4  .isdecimal() 十进制数字(罗马数字不是)
# 5  .isnumeric() 数字(各国语言的数字)
# 6  .isalnum() 全部由字母或数字组成
print('1,','hello,world'.isidentifier()) # False
print('1,','张三2_'.isidentifier()) # True
print('2,','\t'.isspace()) # T
print('3,','hhh'.isalpha()) # T
print('3,','张三'.isalpha()) # T
print('3,','张三2'.isalpha()) # F
print('4,','101'.isdecimal()) # T
print('4,','ⅠⅡⅣ'.isdecimal()) # F
print('4,','八八六'.isdecimal()) # F
print('5,','ⅠⅡⅣ'.isnumeric()) # T
print('5,','八八六'.isnumeric()) # T
print('6,','ab78张'.isalnum()) # T
print('6,','!。'.isalnum()) # F

# 字符串替换合并 .replace() .join()
s = 'hello Python Python Python'
print(s.replace('Python','Java'))   # hello Java Java Java
print(s.replace('Python','Java',2)) # hello Java Java Python
print('*'.join('Python'))  # P*y*t*h*o*n
lst = ['hello','Python','world']
print('|'.join(lst))       # hello|Python|world
t = ('hello','Python','world')
print(''.join(t))          # helloPythonworld

# ==================== 字符串的比较
# 一个一个字符比较,比的是原始值ordinal value
print('apple' > 'app')    # T
print('apple' < 'banana') # T
print(ord('a'), ord('b')) # 97 98
print(chr(97), chr(98))   # a b
# 再次强调:== 与 is 区别 :==比较value is比较id地址

# ==================== 字符串的切片[start,end,step]
s = 'hello,Python'
s1 = s[:5] # hello
s2 = s[6:] # Python
s3 = '!'
newstr = s1+s3+s2
print(newstr) # hello!Python
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr)) # 以上地址不同
s = '0123456789'
print(s[1:9:2]) # 1357
print(s[::2])   # 02468
print(s[::-2])  # 97531
s = 'hello,Python'
print(s[-6::1]) # Python

# ==================== 格式化字符串
# 1,%占位符 %s:字符串 %i/%d:整数 %f:浮点数
# 2,{}占位符 .format()
# 输出:我叫张三,今年21周岁
name = '张三'
age = 21
print('我叫%s,今年%i周岁' % (name,age))
print('我叫{0},今年{1}周岁'.format(name,age))
print(f'我叫{name},今年{age}周岁')
print('%10f' % 3.1415926)     #  3.141593
print('%10.3f' % 3.1415926)   #     3.142
print('%-10.3f' % 3.1415926)  # 3.142
print('{0}'.format(3.1415926))      # 0表示占位符,可以不写
print('{:.3}'.format(3.1415926))    # 3.14   .3表示是三位数
print('{:.3f}'.format(3.1415926))   # 3.142 .3f表示3位小数
print('{:10.3}'.format(3.1415926))  #       3.14 10表示宽度

# ==================== 字符串的编码转换 .encode() .encode()
# 编码.encode() 解码.encode()
s = '天涯共此时'
print(s.encode(encoding='GBK'))   # 在GBK这种编码格式中,一个中文占2字节
print(s.encode(encoding='UTF-8')) # 在UTF-8编码格式中,一个中文占3字节
# so,不同的编码格式决定了它占的字节数
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、付费专栏及课程。

余额充值