Question:

Write the output displayed on execution of the following Python code:
LS = ["HIMALAYA", "NILGIRI", "ALASKA", "ALPS"]
D = {}
for S in LS:
    if len(S) % 4 == 0:
        D[S] = len(S)
for K in D:
    print(K, D[K], sep = "#")
    

Show Hint

Use the len()} function to calculate the length of strings, and the \%} operator to check divisibility conditions in Python loops.
Updated On: Jan 21, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Output:
HIMALAYA#8
ALPS#4
    
Explanation: The list LS contains the strings: ["HIMALAYA", "NILGIRI", "ALASKA", "ALPS"]. The code iterates through each string in LS. If the length of the string is divisible by 4, it adds the string as a key and its length as the value in dictionary D. The dictionary after execution becomes: {"HIMALAYA": 8, "ALPS": 4}. Finally, the dictionary keys and values are printed with a # separator.
Final Output:
HIMALAYA#8
ALPS#4
    
Was this answer helpful?
0
0