CBSE Class 12 Computer Science Question 75 of 105

Python Revision Tour II — Question 2

Back to all questions
2
Question

Question 1(b)

What will be the output produced by following code fragments ?

x = "hello" + \   
"to Python" + \  
"world"  
for char in x :  
    y = char  
    print(y, ':', end=" ")
Answer
Output
h : e : l : l : o : t : o :   : P : y : t : h : o : n : w : o : r : l : d :
Explanation

The code concatenates three strings "hello", "to Python", and "world" into the variable x. Then, it iterates over each character in x using a for loop. For each character, it assigns it to the variable y and prints y followed by a colon and space, all on the same line due to the end=" " parameter.