在Python中将字符串转换为标题大小写
标题大小写是一种写作风格,用于文章、书籍、电影和其他作品的标题。标题大小写的规则是:
1) 始终将第一个单词大写。
2) 将除以下词性外的所有单词大写:
- 文章 – a, an, the
- 并列连词——and, but, for, nor, or, so, yet
- 短介词 – to, in, up, from 等
例子:
Input : The quick brown fox jumps over the lazy dog.
Output : The Quick Brown Fox Jumps over the Lazy Dog.
Input : A tale of two cities
Output : A Tale of Two Cities
算法 :
- 列出所有必须小写的单词。
- 对于输入中的每个单词,检查它是否在上面的列表中。
- 如果是,则忽略该单词,如果不是,则将其第一个字符大写。
上述算法的实现是:
# Function to convert into title case
def generateTitleCase(input_string):
# list of articles
articles = ["a", "an", "the"]
# list of coordinating conjunctins
conjunctions = ["and", "but",
"for", "nor",
"or", "so",
"yet"]
# list of some short articles
prepositions = ["in", "to", "for",
"with", "on", "at",
"from", "by", "about",
"as", "into", "like",
"through", "after", "over",
"between", "out", "against",
"during", "without", "before",
"under", "around", "among",
"of"]
# merging the 3 lists
lower_case = articles + conjunctions + prepositions
# variable declaration for the output text
output_string = ""
# separating each word in the string
input_list = input_string.split(" ")
# checking each word
for word in input_list:
# if the word exists in the list
# then no need to capitalize it
if word in lower_case:
output_string += word + " "
# if the word does not exists in
# the list, then capitalize it
else:
temp = word.title()
output_string += temp + " "
return output_string
# Driver code
if __name__=='__main__':
input_text1 = "The quick brown fox jumps over the lazy dog."
input_text2 = "A tale of two cities"
print(generateTitleCase(input_text1))
print(generateTitleCase(input_text2))
输出 :
The Quick Brown Fox Jumps over the Lazy Dog.
A Tale of Two Cities