Home > Software engineering >  How to make sure the beginning of two strings match until the last word(s)?
How to make sure the beginning of two strings match until the last word(s)?

Time:06-23

So I have two strings:

failure = ""
success = "Today's day is Wednesday"

The failure string is an empty string but must end up to be exactly identical to the success string (for comparison reasons later down). The tricky part is, you can't just set them equal to each other and the success strings ends differently depending on the day. So while the success string changes, the failure string might not change (when it should).

Ultimately, the goal is the compare the two strings and make sure they are always equal no matter what day it is:

if (failure == success):

How do I make sure that the beginning of both the failure string and the success string matches word for word until the last word (which would be the day)? I thought about making a variable that we pass-in for the day. Something like day_name and use subtext but not sure how to execute that

CodePudding user response:

Remove the last word from success and assign to failure:

import re

success = "Today's day is Wednesday";
failure = re.sub(r'\w $', '', success)

CodePudding user response:

If you want failure to be the same as success, just do:

failure = success

this will change failure to be the same as succes. You can also check if failure is not the same as succes:

if failure != success:
    failure = success #change failure into success.
  • Related