Question

In: Computer Science

Write a Python loop that goes through the list and prints each string where the string...

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'].

Solutions

Expert Solution

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


Related Solutions

Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each...
Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each character letter in the string. The actual print statement is provided. # What goes here? print(letter) Checkpoint 8.11 Write an if statement using the in operator that determines whether 'd' is in my_string. A print statement that might be placed after the if statement is included. # What goes here? print('Yes, it is there.') Checkpoint 8.13 Write the first if statement to complete the...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
PYTHON 1. Write a for loop to iterate across a user entered string in reverse order...
PYTHON 1. Write a for loop to iterate across a user entered string in reverse order Use an input statement to ask the user for a string using the prompt: Cheer? [space after the ?] and assign to a variable Start the for loop to iterate across the elements in the string in reverse order Display each element of the string on screen After the for loop ends, display the entire string 2. Write a for loop with a range...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
python question: Problem Statement Given a list of integers input_list, loop through every element in the...
python question: Problem Statement Given a list of integers input_list, loop through every element in the list to find the product of all positive integers and the count of all negative integers. The code to get the input_list is provided for you. The first line of code provided gets the size of the list. The remaining lines of code provided get the elements of the list. The provided data preprocessing code reads in these values and creates a list of...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT