91.2k views
5 votes
Hi, I'm doing Code HS right now and I need someone who understands python to help me write a working code, I've tried numerous times and it keeps showing incorrect on CodeHS.

These are the directions for "Exclamation Points"

Words are way more edgy when you replace the letter i with an exclamation point!

Write the function exclamations that takes a string and then returns the same string with every lowercase i replaced with an exclamation point. Your function should:

Convert the initial string to a list
Use a for loop to go through your list element by element
Whenever you see a lowercase i, replace it with an exclamation point in the list
Return the stringified version of the list when your for loop is finished

Thank you for your time!!

User Slick
by
7.4k points

1 Answer

5 votes

Answer:

Here's a Python code that should accomplish the task described in the directions:

Copy code

def exclamations(s):

s = list(s)

for i in range(len(s)):

if s[i] == 'i':

s[i] = '!'

return ''.join(s)

This code first converts the input string to a list so that individual characters can be accessed and modified. Then, it uses a for loop to go through each element of the list (in this case, each character of the original string). If the current character is a lowercase i, it is replaced with an exclamation point. Finally, the modified list is converted back to a string and returned.

The key is to know that the string type in python is immutable and you can't modify it by index like you do with list, you need to convert it into a list to be able to modify its elements.

It's also good to check if the string is converted to lowercase before the comparision, this way you could replace all the possible 'i' that could be in uppercase too.

Step-by-step explanation:

User Alfcope
by
7.1k points