大小写交替

萨阿德·阿什拉夫

我在处理某些代码时遇到问题:

我正在尝试编码,以便在使用索引号的情况下交替使用句子的字母。

例如:

input:hello im an idiot
output: ToIdI nA mI oLlEh
考希克NP

就像这样简单:

>>> 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章