我们可以编写一个函数,循环遍历字符串列表中的每个字符串,使用Python内置的title()
函数来将它们大写为标题形式,并将修改后的字符串添加到一个新的列表中,最后返回这个新列表即可。
示例代码如下:
def title_case(strings):
title_strings = []
for string in strings:
title_strings.append(string.title())
return title_strings
在这个示例中,我们定义了一个名为title_case()
的函数,它接受一个参数strings
,它是要大写为标题形式的字符串列表。我们创建了一个名为title_strings
的新列表,用于存储修改后的标题字符串。接下来,我们使用for
循环遍历strings
列表中的每个字符串,并使用title()
函数将其大写为标题形式,并将其添加到title_strings
列表中。最后,我们使用return
语句返回title_strings
列表。
示例运行代码:
strings = ["the lord of the rings", "the shawshank redemption", "the dark knight"]
title_strings = title_case(strings)
print(title_strings)
输出结果:['The Lord Of The Rings', 'The Shawshank Redemption', 'The Dark Knight']
上一篇:编写一个函数,接收一个字符串列表,并将每个字符串在一个矩形框架中单独打印出来。
下一篇:编写一个函数,接受一个字符串数组作为参数,并返回一个筛选后的数组,其中仅包含原始数组中的元素,但已将其它元素删除。