In: Computer Science
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
#lists.
list1=['abab', 'xyz', 'aa', 'x', 'bcb']
list2=['', 'x', 'xy', 'xyx', 'xx']
list3=['aaa', 'be', 'abc', 'hello']
#list1.
print("list1: ",end="")
for string in list1: #passing strings from list1.
if(len(string)>=3): #condition string length is >=3.
if(string[0]==string[(len(string)-1)]): #condition first and last
characters of the string same.
print(string) #print string
#list2
print("list2: ",end="")
for string in list2: #passing strings from list3.
if(len(string)>=3): #condition string length is >=3.
if(string[0]==string[(len(string)-1)]): #condition first and last
characters of the string same.
print(string) #print string
#list3
print("list3: ",end="")
for string in list3: #passing strings from list3.
if(len(string)>=3): #condition string length is >=3.
if(string[0]==string[(len(string)-1)]): #condition first and last
characters of the string same
print(string) #print string
OUTPUT:
SCREENSHOT OF THE CODE:
EXPLANTATION:
I WROTE THE CODE FOR EACH LIST
BUT NO NEED TO WRITE CODE FOR EACH LIST