question archive 1

1

Subject:Computer SciencePrice:3.87 Bought7

1. Consider the loop from Section 8.3 of your textbook.

prefixes = 'JKLMNOPQ'

suffix = 'ack'

for letter in prefixes:

   print(letter + suffix)

Put this code into a Python script and run it. Notice that it prints the names "Oack" and "Qack".

Modify the program so that it prints "Ouack" and "Quack" but leaves the other names the same.

Include the modified Python code and the output in your submission.

2. Give at least three examples that show different features of string slices. Describe the feature illustrated by each example. 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

1)

code

prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
if(letter=='O' or letter=='Q'):
print(letter +'u'+suffix)
else:
print(letter + suffix)
output

code snaps

2)

Python strings are sequences of individual characters, and sharetheir basic methods of access with those other Python sequences - lists and tuples. The simplest way of extracting single characters from strings (and individual members from any sequence) is to unpack them into corresponding variables.

You access the individual characters of a string by using Python's array-like indexing syntax. Here, as with all sequences, it's important to  remember that indexing is zero-based; that is, the first item in the sequence is number 0.

If you want to start counting from the end of the string, instead of the beginning, use a negative index. For example, an index of -1 refers to the right-most character of the string.

example:-

if you want to extract a chunk of more than one character, with known position and size? then you have to specify the start and end position in the bracket like below

 example:-

the last but not the lease variation :square-bracket syntax is to add a third parameter, which specifies the 'stride', or how many characters you want to move forward after each character is retrieved from the original string. The first retrieved character always corresponds to the index before the colon; but thereafter, the pointer moves forward however many characters you specify as your stride, and retrieves the character at that position. And so on, until the ending index is reached or exceeded. If, as in the cases we've met so far, the parameter is omitted, it defaults to 1, so that every character in the specified segment is retrieved.

example:-

please use this google drive link to download the answer file.

https://drive.google.com/file/d/1Q0p6og0UHrFeHlabquEPjq6yjCzdW2Tn/view?usp=sharing

note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link