我希望每个人都是安全的。
我正在尝试遍历字符串,并大写字符串的每个首字母。我知道我可以使用.title(),但是a)在这种情况下,我想弄清楚如何使用大写字母或其他内容-基本知识b)测试中的字符串,并带有一些带有(')的单词,这使得.title() (')后面的字母混淆并大写。
def to_jaden_case(string):
appended_string = ''
word = len(string.split())
for word in string:
new_word = string[word].capitalize()
appended_string +=str(new_word)
return appended_string
问题是解释器给我“ TypeError:字符串索引必须是整数”,即使我在'word'中输入了整数。有帮助吗?
谢谢!
from re import findall
def capitalize_words(string):
words = findall(r'\w+', string)
for word in words:
string = string.replace(word, word.capitalize())
return string
这只是获取字符串中的所有单词,然后替换原始字符串中的单词
您正在使用字符串索引列表。
def to_jaden_case(string):
appended_string = ''
for word in string.split():
new_word = word.capitalize()
appended_string +=str(new_word)
return appended_string