#!/usr/bin/python
# -*- coding: utf-8 -*-
def to_weird_case(string):
lines = string.split()
new_word = ''
new_line = ''
for word in lines:
for item in word:
if word.index(item) %2 ==0:
item = item.upper()
new_word += item
else:
new_word += item
new_line = new_word +' '
return new_line
print to_weird_case('what do you mean')
我想要得到WhAt Do YoU MeAn
,而不是我得到WhAtDoYoUMeAn
。我已經添加了行new_line = new_word +' '
。我的問題在哪裏?無法正確處理大寫字符串
它工作!我一定很困惑把'new_line'放在哪裏。 –
但是如果我想省略最後的空間呢。它會打印出一些類似於'WhAt Do YoU MeAn'的文字,在句尾有額外的空間。 –
您可以在'返回'之前刪除最後一個空格。雖然不是一個美麗的解決方案:) – Psytho