def to_weird_case(string): num = 0 new_word = [] for i in string: if num % 2 == 0: num += 1 i.upper() new_word.append(i) else: num += 1 i.lower() new_word.append(i) print(new_word) to_weird_case('Test for test') 

Greetings to all, I ran into the task of changing the case of characters so that from this line: Test for test turned out: TeSt FoR tEsT . The functions upper () and lower () do not change case. What's the catch? Changed the result that should get, litter did not see, you need to make the first character uppercase, second lowcase, etc. in turn!

 def to_weird_case(string): num = 0 new_word = [] for i in string: if num % 2 == 0: num += 1 new_word.append(i.upper()) else: num += 1 new_word.append(i.lower()) print(''.join(new_word)) 

With your help, the code works, thanks! There is a task to implement words with spaces.

 to_weird_case('Weird string case') # => returns 'WeIrD StRiNg CaSe' 

I can break a line like this:

 s = 'Weird string case' l = s.split() s1 = '' for i in l: s1 += i + ' ' print(s1) 

And then for each word to apply the previous code, but it is clear that this is not the right decision, tell me how to implement?

  • one
    i.lower() and i.upper() do not change the original string, but return the modified string. In your code, you just need to do new_word.append(i.lower()) and new_word.append(i.upper()) . Well, then when turning new_word turn it from list to line: ''.join(new_word) . - insolor 2:51 pm
  • Thanks for the answer! Now I understand what the error is) - maximus

2 answers 2

The new version, which breaks the line into words:

step by step - the function involved in changing the register in the word:

 def to_weird_case2(sss): return ''.join([c.upper() if i % 2 == 0 else c.lower() for i,c in enumerate(sss)]) 

application of this function using "list comprehension":

 sss = 'Weird string case' ' '.join([to_weird_case2(w) for w in sss.split()]) 

Test:

 In [76]: ' '.join([to_weird_case2(w) for w in sss.split()]) Out[76]: 'WeIrD StRiNg CaSe' 

A monolithic function that does everything at once (more versatile, of course, have small functions — building-blocks, each of which deals with its one task)

 def to_weird_case(sss): return ' '.join([ ''.join([ c.upper() if i % 2 == 0 else c.lower() for i,c in enumerate(s) ]) for s in sss.split() ]) 

Test:

 In [70]: to_weird_case('Weird string case') Out[70]: 'WeIrD StRiNg CaSe' 

Old version:

if you need to swap upper and lower case:

 In [11]: s Out[11]: 'Test for test' In [12]: new = ''.join([x.lower() if x.isupper() else x.upper() for x in s]) In [13]: new Out[13]: 'tEST FOR TEST' 

if you want to convert to uppercase all characters with even indices and to lower with odd ones:

 In [37]: new = ''.join([c.upper() if i % 2 == 0 else c.lower() for i,c in enumerate(s)]) In [38]: new Out[38]: 'TeSt fOr tEsT' 
  • Thank you very much! - maximus
  • one
    Preferably, only a step-by-step version is shown for an example instead of a monolithic function. Here's another option: words = text.split(); print(*map(interleave_case, words)) words = text.split(); print(*map(interleave_case, words)) , where interleave_case = lambda s: ''.join(map(''.join, zip_longest(s[::2].upper(), s[1::2].lower(), fillvalue=''))) - jfs
  • @jfs, thanks for the good comment! I think you should put this decision in the form of a separate answer, because he seems much better than mine. I had a similar idea yesterday (using s[::2].upper() , s[1::2].lower() with zip() and map() ), but I never managed to get it to work properly zip() for strings with an odd length - about zip_longest() I completely forgot ... - MaxU
  • For a separate answer, the code from my comment is too complicated for the probable readers of this question (beginners in both Python and programming). If you wish, add this solution with an explanation to your answer. - jfs
  • one
    It will be great if you write your own implementation, I’m still superficially familiar with the lambda & map, but sooner or later I will return to this issue and will definitely come in handy. In addition, you can help and more experienced, I welcome your decision with a separate answer) - maximus

Here is my way (more understandable, easily changeable):

 string = "Test for test" new_string = "" for i in string: if i.isupper(): new_string += i.lower() else: new_string += i.upper() 

More about the object's string methods here.