Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23596

IslandT: Capitalize the letters that occupy even indexes and odd indexes separately

$
0
0

Given a string, capitalize the letters within the string that occupy even indexes and odd indexes separately, and return as a list! Index 0 will be considered even.

For example, capitalize(“abcdef”) = [‘AbCdEf’, ‘aBcDeF’]!

The input will be a lowercase string with no spaces.

def capitalize(s):
    s = list(s)
    li = []
    stri = ''
    n = 1
    first = False
    time = 0
    while(time < 2):

        if first == False:
            for e in s:
                if n % 2 != 0:
                    stri += e.upper()
                else:
                    stri += e
                n+=1
            first = True
            n = 1
            li.append(stri)
            stri = ''
            time += 1
        else:
            for e in s:
                if n % 2 == 0:
                    stri += e.upper()
                else:
                    stri += e
                n+=1
            li.append(stri)
            time += 1
    return li

Do you know we actually can achieve the above outcome with just 3 lines of code? Provide your answer in the comment box below!


Viewing all articles
Browse latest Browse all 23596

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>