CBSE Class 12 Computer Science Question 74 of 105

Python Revision Tour II — Question 1

Back to all questions
1
Question

Question 1(a)

What will be the output produced by following code fragments ?

y = str(123)  
x = "hello" \* 3  
print(x, y)  
x = "hello" + "world"  
y = len(x)  
print(y, x)
Answer
Output
hellohellohello 123
10 helloworld
Explanation

str(123) converts the number 123 to string and stores in y so y becomes "123". "hello" * 3 repeats "hello" 3 times and stores it in x so x becomes "hellohellohello".

"hello" + "world" concatenates both the strings so x becomes "helloworld". As "helloworld" contains 10 characters so len(x) returns 10.