Python-函数-字符串函数

乎语百科 367 0
函数
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

留言评论

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~