Computer Science
What will be the output of the following code?
Text = "Mind@Work!"
ln = len(Text)
nText = ""
for i in range(0, ln):
if Text[i].isupper():
nText = nText + Text[i].lower()
elif Text[i].isalpha():
nText = nText + Text[i].upper()
else:
nText = nText + 'A'
print(nText)
Python
Python String Manipulation
4 Likes
Answer
mINDwORKA
Working
Below is the detailed step by step explanation of the program:
Initialization
Text = "Mind@Work!"
ln = len(Text)
nText = ""
Text
is initialized with the string"Mind@Work!"
.ln
holds the length ofText
, which is10
in this case.nText
is initialized as an empty string, which will hold the transformed characters.
Loop through each character
for i in range(0, ln):
if Text[i].isupper():
nText = nText + Text[i].lower()
elif Text[i].isalpha():
nText = nText + Text[i].upper()
else:
nText = nText + 'A'
- A
for
loop iterates over each indexi
from0
toln-1
(i.e.,0
to9
).
Conditions within the loop:
Text[i].isupper()
: Checks if the character at indexi
in the stringText
is an uppercase letter.
- If true, it converts the character to lowercase using
Text[i].lower()
and appends it tonText
.
elif Text[i].isalpha()
: Checks if the character at indexi
in the stringText
is an alphabetic letter (either uppercase or lowercase).
- If the character is alphabetic but not uppercase, it converts the character to uppercase using
Text[i].upper()
and appends it tonText
.
else
statement of loop:
- The
else
statement in this context is associated with thefor
loop, meaning it executes after the loop has completed iterating over all the indices. nText = nText + 'A'
appends the character'A'
to the end ofnText
.
Printing the result
print(nText)
- This line prints the final value of
nText
.
Step-by-Step Execution
1. Initial States:
Text = "Mind@Work!"
ln = 10
nText = ""
2. Loop Iterations:
Index | Character | Condition | Transformation | nText |
---|---|---|---|---|
0 | 'M' | isupper() is True | 'm' | "m" |
1 | 'i' | isalpha() is True | 'I' | "mI" |
2 | 'n' | isalpha() is True | 'N' | "mIN" |
3 | 'd' | isalpha() is True | 'D' | "mIND" |
4 | '@' | neither condition is True | - | "mIND" |
5 | 'W' | isupper() is True | 'w' | "mINDw" |
6 | 'o' | isalpha() is True | 'O' | "mINDwO" |
7 | 'r' | isalpha() is True | 'R' | "mINDwOR" |
8 | 'k' | isalpha() is True | 'K' | "mINDwORK" |
9 | '!' | neither condition is True | - | "mINDwORK" |
3. Post Loop Execution:
- The loop is complete, so the
else
statement appends'A'
tonText
. nText = "mINDwORK" + "A" = "mINDwORKA"
4. Print Output:
print(nText)
outputs"mINDwORKA"
.
Answered By
3 Likes
Related Questions
Write the Python statement and the output for the following:
(a) Find the third occurrence of 'e' in 'sequence'.
(b) Change the case of each letter in string 'FuNcTioN'.
(c) Whether 'Z' exists in string 'School' or not.
Consider the string str1 = "Global Warming".
Write statements in Python to implement the following:
(a) To display the last four characters.
(b) To replace all the occurrences of letter 'a' in the string with "*".
Input the string 'My School'. Write a script to partition the string at the occurrence of letter 'h'.
Write a program to convert a string with more than one word into titlecase string where string is passed as parameter. (Titlecase means that the first letter of each word is capitalized.)