如何反轉字串中的每個單詞,并且python中每個單詞的第一個字母大寫?
input = 'this is the best'
output = Siht Si Eht Tseb
uj5u.com熱心網友回復:
使用split,然后反轉字串,最后capitalize:
s = 'this is the best'
res = " ".join([si[::-1].capitalize() for si in s.split()])
print(res)
輸出
Siht Si Eht Tseb
uj5u.com熱心網友回復:
做就是了:
" ".join([str(i[::-1]).title() for i in input.split()])
uj5u.com熱心網友回復:
這里的其他答案也適用。但我認為這會更容易理解。
s = 'this is the best'
words = s.split() # Split into list of each word
res = ''
for word in words:
word = word[::-1] # Reverse the word
word = word.capitalize() ' ' # Capitalize and add empt space
res = word # Append the word to the output-string
print(res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326794.html
