绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Python-函数-字符串函数
2022-09-26 17:03:12
函数
1.字符串函数
复制代码
#(1)add() 对两个数组的元素进行字符串连接
import numpy as np
print(np.char.add(["xiaodu"],["good"]))
print(np.char.add(["xiaodu","dudu"],["good","nice"])) 
#output:['xiaodugood'] ['xiaodugood' 'dudunice'] 
#(2)multiply() 返回按元素多重连接后的字符串
import numpy as np
print(np.char.multiply("good",3))
#output:goodgoodgood
#(3)center() 将字符串居中,并使用指定字符在左侧和右侧进行填充
import numpy as np
print(np.char.center(["nice","good"],12,fillchar="*"))
#output:['****nice****' '****good****']
#(4)capitalize() 将字符串的个字母转换为大写
import numpy as np
print(np.char.capitalize(["what a nice day today","i have a good time"]))
#output:['What a nice day today' 'I have a good time']
#(5)title() 将字符串的每个单词的个字母转换为大写
import numpy as np
print(np.char.title(["what a nice day today","i have a good time"]))
#output: ['What A Nice Day Today' 'I Have A Good Time']
#(6)lower() 数组元素转换为小写
import numpy as np
print(np.char.lower(["what a nice Day today","i HAVE a good time"]))
#Output:['what a nice day today' 'i have a good time']
#7)upper() 数组元素转换为大写
import numpy as np
print(np.char.upper(["what a nice Day today","i HAVE a good time"]))
#Output: ['WHAT A NICE DAY TODAY' 'I HAVE A GOOD TIME']
#(8)split() 指定分隔符对字符串进行分割,并返回数组列表
import numpy as np
print(np.char.split(["what a nice day today","i have a good time"],sep=" "))
#Output: [list(['what', 'a', 'nice', 'day', 'today'])
 list(['i', 'have', 'a', 'good', 'time'])]
#(9)splitlines() 返回元素中的行列表,以换行符分割
import numpy as np
print(np.char.splitlines(["what a nice\nday today","i have a good\ntime"]))
#output: [list(['what a nice', 'day today']) list(['i have a good', 'time'])]
#(10)strip() 移除元素开头或者结尾处的特定字符
import numpy as np
print(np.char.strip(["**nice***","*day"],"*"))
#output: ['nice' 'day']
#(11)join() 通过指定分隔符来连接数组中的元素
import numpy as np
print(np.char.join("-",["nice","day"]))
print(np.char.join(["-",":"],["nice","day"]))
#output: ['n-i-c-e' 'd-a-y']
['n-i-c-e' 'd:a:y']
#(12)replace() 使用新字符串替换字符串中的所有子字符串
import numpy as np
print(np.char.replace(["what a good day today","i have a good time"],"good","nice"))
#output: ['what a nice day today' 'i have a nice time']
#(13)encode() 编码,数组元素依次调用str.encode
import numpy as np print(np.char.encode(["nice","good"],"utf-8"))
#(14)decode() 解码,数组元素依次调用str.decode
import numpy as np
a = np.char.encode(["nice","good"],"utf-8")
print(np.char.decode(a,"utf-8"))
#output: ['nice' 'good']
分享好友

分享这个小栈给你的朋友们,一起进步吧。

Python从入门到入坑
创建时间:2022-03-24 18:24:00
本小栈包含:Python基本教程 ,数据库基本教程,Python全栈,爬虫。以及Python的电子书分享等
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • 崔浩月1
    栈主

小栈成员

查看更多
  • 只要快乐
  • LCR_
  • ㅤㅤ泳ㅤ
  • Soul FiGuRe丶东
戳我,来吐槽~