Python-函数-字符串函数

函数1.字符串函数#(1)add() 对两个数组的元素进行字符串连接import numpy as npprint(np.char.add(["xiaodu"],["good"]))print(np.char.add(["xiaodu","dudu"],["good","nice"])) #output:['xiaodugood'] ['xiaodugood' 'dudunice'] #(2)multiply() 返回按元素多重连接后的字符串import numpy as npprint(np.char.multiply("good",3))#output:goodgoodgood#(3)center() 将字符串居中,并使用指定字符在左侧和右侧进行填充import numpy as npprint(np.char.center(["nice","good"],12,fillchar="*"))#output:['****nice****' '****good****']#(4)capitalize() 将字符串的第一个字母转换为大写import numpy as npprint(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 npprint(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 npprint(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 npprint(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 npprint(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 npprint(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 npprint(np.char.strip(["**nice***","*day"],"*"))#output: ['nice' 'day']#(11)join() 通过指定分隔符来连接数组中的元素import numpy as npprint(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 npprint(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.encodeimport numpy as np print(np.char.encode(["nice","good"],"utf-8"))#(14)decode() 解码 , 数组元素依次调用str.decodeimport numpy as npa = np.char.encode(["nice","good"],"utf-8")print(np.char.decode(a,"utf-8"))#output: ['nice' 'good']【Python-函数-字符串函数】

    推荐阅读