Skip to content

Instantly share code, notes, and snippets.

@Pierian-Data
Created March 1, 2018 17:23
Show Gist options
  • Save Pierian-Data/5767f49f825dbc9f9bf1357b2152b010 to your computer and use it in GitHub Desktop.
Save Pierian-Data/5767f49f825dbc9f9bf1357b2152b010 to your computer and use it in GitHub Desktop.
def myfunc(x):
out = []
for i in range(len(x)):
if i%2==0:
out.append(x[i].lower())
else:
out.append(x[i].upper())
return ''.join(out)
@LAMRA7
Copy link

LAMRA7 commented Dec 13, 2023

The Answer that me follow
Screenshot (796)
Screenshot (797)

@harshinimamidipalli
Copy link

def myfunc(x):
out = []
for i in range(len(x)):
if i%2==0:
out.append(x[i].lower())
else:
out.append(x[i].upper())
return ''.join(out)

@aksgrades
Copy link

aksgrades commented Dec 16, 2023

def myfunc(text):
    w = [letter for letter in text]
    for letter in w:
        index = w.index(letter)
        if index % 2 == 0:
            w[index] = letter.upper()
    return ''.join(w)

@AHASANUL300-maker
Copy link

def myfunc(args):
new_str = ""
for i in range(len(args)):
if i%2 == 0:
new_str += args[i].lower()
else:
new_str += args[i].upper()

return new_str

@Mozami350
Copy link

def skyline_function(string1):
stringmatch = ""
for i,letter in enumerate(string1):
if (i+1) % 2 == 0:
stringmatch += letter.upper()
else:
stringmatch += letter.lower()
return stringmatch

@RAVITEJAVANUM
Copy link

def myfunc(string):
my = ''
for a in range(len(i)):
if a%2==0:
# concatenate my with each changed letter in input
my+=i[a].lower()
else:
my+=i[a].upper()
return my

@prem465
Copy link

prem465 commented Feb 4, 2024

def myfunc(x):
out = []
for i in range(len(x)):
if i%2==0:
out.append(x[i].lower())
else:
out.append(x[i].upper())
return ''.join(out)

@Zarathustra17
Copy link

Zarathustra17 commented Feb 12, 2024

def myfunc(string):
    
    new_string = ''
    
    for num in range(int(0.5*len(string))):
        new_string = new_string + string[2*num].lower() + string[2*num + 1].upper()
    
    if len(string) % 2 == 1:
        new_string = new_string + string[-1].lower()
    
    return new_string

@SubashriR-DS
Copy link

def myfuc(word):
word_letter =[ ]
for l in range(len(word)):
if (l+1) % 2==0:
word_letter+=word[l].upper()
else:
word_letter+=word[l].lower()
return word_letter

output:
myfuc('Anthropomorphism')

['a',
'N',
't',
'H',
'r',
'O',
'p',
'O',
'm',
'O',
'r',
'P',
'h',
'I',
's',
'M']

@Devansh-274
Copy link

#WORKING CODE.
def myfunc(a):
d=[]
y=0
for i in a:
y= y+1
if y%2 !=0 :
v=i.lower()
d.append(v)
else:
f=i.upper()
d.append(f)
str = ''
for item in d:
str = str + item
return str

@hongzhan2015
Copy link

a bit different

def myfunc(words):
new_words = ''
for index,letter in enumerate(words,1):
if (index + 1) % 2 == 0:
new_words = new_words + letter.upper()
else:
new_words = new_words + letter.lower()
return new_words

@nikhiilss
Copy link

def myfunc(string):
output = ''
for index,letter in enumerate(string):
if index % 2 == 0:
output += letter.upper()
else:
output += letter.lower()
return output

@Aixa-Cal
Copy link

def myfunc(x):

out=[]
for i in range (len(x)):
if i%2 == 0:
out.append x[I].lower()

else:
out.append x[I].upper()

reutn ' '.join(out)

@fazeelzama20
Copy link

fazeelzama20 commented Mar 18, 2024

I think this approach is much better than using the len property inside the range
@Pierian-Data
def myfunc(*args):
str = ''
for key,value in enumerate(args[0]):
if key %2 == 0:
str += value.upper()
else:
str += value.lower()
return str

@sudheerkilani
Copy link

image

@akv
Copy link

akv commented Mar 25, 2024

def myfunc(my_string):
    output = []
    for i,w in enumerate(my_string):
        if i % 2 ==0:
            output.append(w.upper())
        else:
            output.append(w.lower())
    return ''.join(output)

@Shanefdo
Copy link

image

@Phan-Hoangg
Copy link

def myfunc(x):
out = []
for i in range(len(x)):
if i % 2 == 0:
out.append(x[i].lower())
else:
out.append(x[i].upper())
return ''.join(out)
print(myfunc('Hoang'))

@Niku113
Copy link

Niku113 commented Apr 6, 2024

def myfunc(str):
sum = ""
pos = 0
for i in str:
pos+=1
z = pos-1
if z % 2 ==0:
a = str[z].upper()
else:
a = str[z]
sum += a
return(sum)

@Fan0Negative
Copy link

def myfunc(string):
     newstring=''
     for position in range(len(string)):
        if position%2==0:
            newstring+=string[position].upper()
        else:
            newstring+=string[position].lower()
    return newstrin`

@Shanks106-xo
Copy link

def myfunc(name):
lname=name.upper()
l=list(lname)
for i in range(0,len(lname),2):
l[i]=l[i].lower()
return ''.join(l)

@RogelioDaniel
Copy link

def myfunc(argumento):
lista=''
for index in range (len(argumento)):
if index % 2 == 0:
lista+=argumento[index].lower()
else:
lista+=argumento[index].upper()
return lista

@ZhilinShi
Copy link

def myfunc(string2):
chars = [char for char in string2]
i = 0
for j in chars:
if i%2 == 0:
chars[i] = j.lower()
i += 1
else:
chars[i] = j.upper()
i += 1
string2 = ''.join(chars)
return string2

@ZhilinShi
Copy link

def myfunc(x):
out = ''
for i in range(len(x)):
if i%2 == 0:
out += x[i].lower()
else:
out += x[i].upper()
x = out
return x

@JMakumi
Copy link

JMakumi commented May 9, 2024

def myfunc(str):
converted_str= ''
char_counter = 0
for char in str:
if char_counter%2==0:
converted_str = converted_str+char.lower()
else:
converted_str = converted_str+char.upper()
char_counter += 1
return converted_str

@shrutigitam
Copy link

def myfunc(a):
x=''
for index,word in enumerate(a):
if index%2==0:
x =x + word.upper()
else:
x =x+ word.lower()
return x
myfunc('abhishek')

@shrutigitam
Copy link

def myfunc(a):
x=[]
for i in range(len(a)):
if i % 2==0:
x.append(a[i].lower())
else:
x.append(a[i].upper())
return ''.join(x)
myfunc('rita')

@jdavid34
Copy link

def myfunc(s):
new_str = ""
for i in range(len(s)):
if i % 2 == 0:
new_str += s[i].upper()
else:
new_str += s[i].lower()
return new_str

print(myfunc('prueba'))

@nam-vuhoang
Copy link

nam-vuhoang commented Jun 13, 2024

def myfunc(st):
    lst = [char.upper() if index % 2 != 0 else char.lower() for index, char in enumerate(st)]
    return ''.join(lst)

@haneenmuhammed2005
Copy link

def myfunc(x):
out[]
for i in range(len(x)):
if i%2==0:
out.append(x[i].lower())
else:
out.append(x[i].upper())
return' '.join(out)

@tagathi
Copy link

tagathi commented Jul 10, 2024

def myfunc(mystring):
    matching_string = ''
    my_index = 0
    while my_index < len(mystring):
        new_letter = mystring[my_index]
        if my_index%2 == 0:
            matching_string = matching_string + new_letter.lower()
        else:
            matching_string = matching_string + new_letter.upper()
        my_index = my_index + 1 
    return matching_string

@shirel-sanker
Copy link

def myfunc(text):
out=''

for i in range(len(text)):
    if i%2==0:
        out+= text[i].upper()
    else:
        out+= text[i].lower()
        
return out

myfunc('Anthropomorphism')

@masikdev
Copy link

def myfunc(word):
letters = [*word]
result = ""
for index, item in enumerate(letters):
if index % 2 == 0:
result += item.upper()
else:
result += item.lower()
return result

@AarushMalbari
Copy link

def myfunc(st):
#print(st.split())
str1=''
counter=1
for index in st:

    if counter%2==0:
        str1+= index.upper()
    else:
        str1+=index.lower()
    counter+=1
return(str1)

print(myfunc('AnthropomorphismakhfB'))

@Avniarya8
Copy link

def myfunc(*args):
word = ''
for letter in args:
if len(args) % 2 == 0:
word.append(letter.upper)
else:
word.append(letter.lower)

AttributeError Traceback (most recent call last)
Cell In[2], line 1
----> 1 myfunc('Avni')

Cell In[1], line 7, in myfunc(*args)
5 word.append(letter.upper)
6 else:
----> 7 word.append(letter.lower)

AttributeError: 'str' object has no attribute 'append'

@AbhijeetRaaj
Copy link

A another approach that I have tried using args:

def myfunc(*args):
mystr = str(args)
new_list = list(mystr)
length = len(new_list)
order_list = []
final = ''
even = (mystr[0:length:2].lower())
l_even = len(even) - 1
odd = (mystr[1:length:2].upper())
l_odd = len(odd) - 1

while length !=0:
    if length%2 != 0:
        order_list.append(even[l_even])
        l_even = l_even - 1
    else:
        order_list.append(odd[l_odd])
        l_odd = l_odd - 1
    length = length -1
order_list.reverse()
for i in order_list:
    final = final + i
return final

this is good to add any number of arguments that can be fit for this usecase

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment