NumPy title()
函数
在本教程中,我们将介绍 Numpy 库的title()
功能。
title()
功能用于将输入字符串转换为标题大小写版本。
需要注意的是,标题大小写单词总是以大写字符开头,其余所有字符都是小写字符。如果一个字符串有多个单词,那么所有的单词都被转换成标题大小写。例如,如果我们有一个字符串“我爱 StudyTonight”,如果我们在这个字符串上使用 title()函数,它将被更改为“我爱 StudyTonight”。
这个函数在内部为数组的所有元素调用
str.title
。对于 8 位字符串,该函数依赖于区域设置。
numpy.char.title()
的语法:
使用该函数所需的语法如下:
numpy.char.title(arr)
以上语法表明title()
函数取两个参数。
在上面的语法中,参数arr
表示将应用该方法的字符串输入数组。
返回值:
该函数将返回对应于原始字符串的标题字符串。如果您提供一个字符串数组,那么所有的字符串元素都将被更改为 titlecased。
示例 1:使用字符串
下面是一个基本的代码示例,我们在一个线程上使用了title()
函数:
import numpy as np
a = "this is my string"
print("The original string:")
print(a)
print("\n")
print("Applying title() method :")
x = np.char.title(a)
print(x)
原弦: 这是我的弦
施题()法: 这是我的弦
例 2:
在本例中,我们将使用带有大写字符的字符串的title()
函数来查看title()
函数如何改变它:
import numpy as np
a = "Titlecased StrINg "
print("The original string:")
print(a)
print("\n")
print("Applying title() method :")
x = np.char.title(a)
print(x)
原字符串: 标题字符串
应用标题()方法: 标题字符串
示例 3:使用字符串数组
现在让我们使用带有字符串值数组的title()
函数:
import numpy as np
arr = np.array(['what aRE YOUR', 'plans for Tonight', 'will you','study tonight'])
print ("The original Input array : \n", arr)
output = np.char.title(arr)
print ("The output titlecased array: ", output)
原始输入数组: [“你今晚有什么计划”“你今晚会学习吗”] 输出标题数组:【“你今晚有什么计划”“你今晚会学习吗”】
摘要
在本教程中,我们学习了 Numpy 库的title()
功能。我们介绍了如何使用它的语法和这个函数返回的值,以及几个例子,看看这个函数如何处理字符串和字符串数组。