Python学习-Numpy库字符串操作--转换、替换、填充、统计、判断

20 篇文章 6 订阅

Numpy 字符串操作–转换、替换、填充、统计、判断

1、字符串大小写转换
2、字符串子串替换、填充
3、字符串分割
4、子串次数统计、查找
5、字符串判断操作
导入numpy库
import numpy as np
str0 = "ABCDefgh"
1、字符串大小写转换
str1 = str0.lower()  # 大写转小写
str2 = str0.upper()  # 小写转大写
str3 = str0.swapcase()  # 大写转小写,小写转大写
2、新字符代替旧字符、填充
str4 = str0.replace('ABCD', 'qwer')
# print(str1)
字符串居中,指定长度,不足指定字符填充
str5 = str0.center(20, '*')
# print(str5)
3、指定分割符,分割字符串,返回数组
str6 = str0.split(sep='e')  # 默认为空格
# print(str6)
移除字符串头尾的特定字符
str7 = str0.strip('A')
# print(str7)
4、统计子串出现的次数
str8 = str0.count('A')
# print(str8)
查找指定的最左边字符串,返回对应下标
str9 = str0.find('D')
# print(str9)
4、判断字符串
str10 = str0.isalpha()  # 字母
str11 = str0.isdecimal()  # 十进制
# print(str10, str11)
是否为小写、大写
str12 = str0.islower()
str13 = str0.isupper()
# print(str12, str13)
Unicode数字、byte数字(单字节):isdigit()
str14 = str0.isdigit()  # 是否为数字
Unicode数字、全角数字(双字节)、汉字数字
str15 = str0.isnumeric()
# print(str14, str15)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值