CBSE Class 12 Computer Science
Question 91 of 105
Python Revision Tour II — Question 18
Back to all questions 18
Question Write a method in python to display the elements of list thrice if it is a number and display the element terminated with '#' if it is not number.
For example, if the content of list is as follows :
List = ['41', 'DROND', 'GIRIRAJ', '13', 'ZARA']The output should be
414141
DROND#
GIRIRAJ#
131313
ZAR#
def display(my_list):
for item in my_list:
if item.isdigit():
print(item * 3)
else:
print(item + '#')
display(my_list = eval(input("Enter the list :")))Enter the elements of the list separated by spaces:41 DROND GIRIRAJ 13 ZARA
414141
DROND#
GIRIRAJ#
131313
ZARA#
- The code prompts the user to enter the elements of the list separated by spaces and stores the input as a single string in the variable
my_list. - Then splits the input string my_list into individual elements and stores them in a new list called
new_list. - Then for loop iterates over each element in the new_list.
- The
isdigit()method is used to check if all characters in the string are digits. If it's true (i.e., if the element consists only of digits), then it prints the element concatenated with itself three times. Otherwise, if the element contains non-digit characters, it prints the element concatenated with the character '#'.