I have a Python problem where i need to take input as a string in a function and need to return back a string where every alternate letter is a sequence of small case and Capital case. Ex: String passed to the function: AmsTerdam
then the returned string should be AmStErDaM
. It can start with any case i.e., small case or capital case.
I am still in learning phase of Python and have come up with the following but somehow when i try to execute, the code hangs. Could anyone please help me in fixing this?
def myfunc(NAME='AmsTerdam'):
leng=len(NAME)
ind=1
newlist=[]
while ind <= leng:
if ind%2==0:
newlist.append(NAME[ind-1].upper())
else:
newlist.append(NAME[ind-1].lower())
str(mylist) # Can we typecast a list to a string?
return newlist
OUT=myfunc('Ankitkumarsharma')
print('Output: {}'.format(OUT))
If the typecasting cannot be done, is the following correct?
def myfunc(NAME='AmsTerdam'):
leng=len(NAME)
ind=1
newstr=''
while ind <= leng:
if ind%2==0:
newstr=newstr+NAME[ind-1].upper()
else:
newstr=newstr+NAME[ind-1].lower()
return newstr
OUT=myfunc('AmsTerdam')
print('Output: {}'.format(OUT))