What will be the output of the following code?str1="HellO{}niNja"str1 = str1.replace(" {} ", " ")str1=str1.lower()pos=str1.find("o")print(str1+str(pos))
Question
What will be the output of the following code?str1="HellO{}niNja"str1 = str1.replace(" {} ", " ")str1=str1.lower()pos=str1.find("o")print(str1+str(pos))
Solution
The code you provided has a few issues, but I'll explain what it's supposed to do step by step:
-
str1="HellO{}niNja": This line defines a string variablestr1with the value "HellO{}niNja". -
str1 = str1.replace(" {} ", " "): This line is supposed to replace " {} " instr1with a space. However, there is no " {} " instr1, so this line doesn't changestr1. -
str1=str1.lower(): This line convertsstr1to lowercase, sostr1becomes "hello{}ninja". -
pos=str1.find("o"): This line finds the first occurrence of "o" instr1and assigns its index topos. The index of the first "o" in "hello{}ninja" is 4 (since indices start at 0), soposbecomes 4. -
print(str1+str(pos)): This line printsstr1followed bypos, so it prints "hello{}ninja4".
However, the code you provided has a few issues:
- Python code should be indented properly. The code you provided is all on one line, which Python doesn't allow.
- The replace function is looking for " {} ", but there is no " {} " in
str1. It should be looking for "{}" instead.
Here's the corrected code:
str1="HellO{}niNja"
str1 = str1.replace("{}", " ")
str1=str1.lower()
pos=str1.find("o")
print(str1+str(pos))
This code will print "helloninja4".
Similar Questions
What will be the output of below Python code?str1="Aplication"str2=str1.replace('a','A')print(str2)applicationApplicationApplicAtionapplicAtion
What will be the output of the following code?str1=" An apple a day keeps doctor away"print(str1.find("a"),end=" ")print(str1.find("m"),end=" ")print(str1.find("day"),end=" ")str1=str1[ : :-1]print(str1.find("y"),end=" ")
2. What will be the output of the following Python code?>>>str="hello">>>str[:2]>>>
Subject: PythonWhat is the output of below python code?s = 'SIKANDAR's.replace('A','E')print(s)
What will be the output of the program?String x = "xyz";x.toUpperCase(); /* Line 2 */String y = x.replace('Y', 'y');y = y + "abc";System.out.println(y);abcXyZabcxyzxyzabcXyZabc
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.