我在处理某些代码时遇到问题:
我正在尝试编码,以便在使用索引号的情况下交替使用句子的字母。
例如:
input:hello im an idiot
output: ToIdI nA mI oLlEh
就像这样简单:
>>> string='hello im an idiot'
>>> out=''
>>> caps=True #flag to see if upper case
>>> for s in string[::-1]: #string[::-1] reverses it
if s==' ': #when the char is a whitespace, directly add it to the resultant output
out+=' '
continue
if caps: #should be uppercase
out+=s.upper()
caps = False
else: #should be lowercase
out+=s.lower()
caps = True
>>> out
=> 'ToIdI nA mI oLlEh'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句