Can someone let me know if this is the correct way to solve this problem?

###############################################
#       Exercise by Websten from forums       #
###############################################
# To minimize errors when writing long texts with
# complicated expressions you could replace 
# the tricky parts with a marker. 
# Write a program that takes a line of text and 
# finds the first occurrence of a certain marker 
# and replaces it with a replacement text. 
# The resulting line of text should be stored in a variable. 
# All input data will be given as variables.
#
# Replace the first occurrence of marker in the line below.
# There will be at least one occurrence of the marker in the
# line of text. Put the answer in the variable 'replaced'.
# Hint: You can find out the length of a string by command
# len(string). We might test your code with different markers!

# Example 1
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."

# Example 2 # uncomment this to test with different input
#marker = "EY"
#replacement = "Eyjafjallajokull"
#line = "The eruption of the volcano EY in 2010 disrupted air travel in Europe for 6 days."

###
# YOUR CODE BELOW. DO NOT DELETE THIS LINE
###

# Example 1 output should be:
#>>> I will now go to sleep and be away from keyboard until lunch time tomorrow.
# Example 2 output should be:
#>>> The eruption of the volcano Eyjafjallajokull in 2010 disrupted air travel in Europe for 6 days.

---------------------------------------------------------------MY SOLUTION-----------------------------------------------------
marker= "AFK"
replacement= "away from keyboard"
line= "I will now go to sleep and be AFK until lunch time tomorrow."

remaining= line.find(" until")


position= line.find("AFK")

correct= line[:position] + replacement + line[remaining:] 

print correct

marker2= "EY"
replacement2= "Eyjafjallajokul"
line2 = "The eruption of the volcano EY in 2010 disrupted air travel in Europe for 6 days."
remaining2= line2.find(" in")
position2= line2.find("EY")

correct2= line2[:position2] + replacement2 + line2[remaining2:]

print correct2

This looks like a homework or lab assignment which may be proprietary. Not sure you want to be posting it in the forums as your professor can find it with a quick search. If anybody gives you a solution, best not use it or you could be expelled. Just saying…

1 Like

its a free online course.

What method do we know of that can do just as described above, and assign the outcome to a variable?

new_str = old_str._______(marker, replacement_text)

In a dynamic environment we don’t have any familiarity with the text. We’re querying for a known string, but need not know anything else about its parent string. That means we don’t know the next word. All we know is the position of our found string, and its length.

>>> marker= "AFK"
>>> replacement= "away from keyboard"
>>> line = "I will now go to sleep and be AFK until lunch time tomorrow."
>>> x = line.find(marker)
>>> n = len(marker)
>>> f"{line[:x]}{replacement}{line[x+n:]}"
'I will now go to sleep and be away from keyboard until lunch time tomorrow.'
>>> 

That makes a lot of sense thank you so much for the input. I now understand it :smiley: I also had no knowledge about the len function, is very handy.

The len() function is kind of fundamental. One hopes you haven’t skipped over the basics and jumped into the deep end. Could we assume you have not yet got an answer to the earlier question?

1 Like

It was part of the bonus set of questions which do not come for answers so I searched in forums for a little guideline.

1 Like

So instead of the above method where we used .find(), what other approach can we use? The hint is in the earlier post.