python 2.7 - can't get right when dealing with uppercasing string -


#!/usr/bin/python # -*- coding: utf-8 -*- def to_weird_case(string):     lines = string.split()     new_word = ''     new_line = ''     word in lines:         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 mean') 

i want what mean, instead got whatdoyoumean. add line new_line = new_word +' '. problem?

first, overwrite new_line every iteration. second, new_word getting longer because never "clear" it. third, add space end of entire new_line , not after every new word (because of second).

see comments

def to_weird_case(string):     lines = string.split()     new_line = ''     word in lines:         new_word = '' # start new word empty string         item in word:             if word.index(item) %2 ==0:                 item = item.upper()                 new_word += item             else:                 new_word += item         print new_word         new_line = new_line + new_word + " " # add new word existing new line      return new_line 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -