Python Program-1: makenew(str) Program with Explanation



Python Program-1: makenew(str) Program with Explanation | ninjasquad

Question: Find the output of the following Python program.

def makenew(mystr): newstr = " " count = 0 for i in mystr: if count%2 !=0: newstr = newstr+str(count) else: if i.islower(): newstr = newstr+i.upper() else: newstr = newstr+i count +=1 newstr = newstr+mystr[:1] print("The new string is :",newstr) makenew("sTUdeNT")

Code language: Python (python)

Output:

The new string is : S1U3E5Ts

Code language: JavaScript (javascript)

Also Read: How to Convert CSV to TXT in Java?

Explanation:

Iteration-1:

Current Values Operation
mystr = sTUdeNT
newstr = ” ”
count = 0
i = s
As count = 0 and i = s which is small letter, So
newstr = newstr + i.upper()
newstr = ” ” + S = S
count = 1

Iteration-2:

Current Values Operation
mystr = sTUdeNT
newstr = S
count = 1
i = T
As count = 1 and i = T which is a capital letter, So
newstr = newstr + str(count)
newstr = S + str(1) = S1
count = 2

Iteration-3:

Current Values Operation
mystr = sTUdeNT
newstr = S1
count = 2
i = U
As count = 2 and i = U which is a capital letter, So
newstr = newstr+i
newstr = S1 + U = S1U
count = 3

Iteration-4:

Current Values Operation
mystr = sTUdeNT
newstr = S1U
count = 3
i = d
As count = 3 and i = d which is a capital letter, So
newstr = newstr + str(count)
newstr = S1U + 3 = S1U3
count = 4

Iteration-5:

Current Values Operation
mystr = sTUdeNT
newstr = S1U3
count = 4
i = e
As count = 4 and i = e which is a capital letter, So
newstr = newstr + i.upper()
newstr = S1U3 + E = S1U3E
count = 5

Iteration-6:

Current Values Operation
mystr = sTUdeNT
newstr = S1U3E
count = 5
i = N
As count = 5 and i = N which is a capital letter, So
newstr = newstr + str(count)
newstr = S1U3E + 5 = S1U3E5
count = 6

Iteration-7:

Current Values Operation
mystr = sTUdeNT
newstr = S1U3E5
count = 6
i = T
As count = 6 and i = T which is a capital letter, So
newstr = newstr+i
newstr = S1U3E5 + T = S1U3E5T
count = 7

After the iteration completion of for loop, now next line will be executed.

newstr = newstr+mystr[:1]
newstr = S1U3E5 + s = S1U3E5s

Finally, we will display the new String by using the print method of Python. Hence the new String is ” S1U3E5Ts

Python Program Question Source: brainly.in

Related Programs:



Source: Internet

Leave a Comment

We are offering free coding tuts

X