Ask Your Question

Check if a word is inside another word.

asked 2022-07-11 06:21:34 +0100

Fr4n_w4s_h3r3 gravatar image

How can I say if a word is inside another word. By example:

word("11") is inside ("011")

word("11") is not inside ("101")

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2022-07-17 11:52:44 +0100

Sébastien gravatar image

updated 2022-07-17 11:53:44 +0100

You may use the method is_factor:

sage: w = Word('11')
sage: w.is_factor(Word('011'))
sage: w.is_factor(Word('101'))

To find the first occurrence, you need to reverse the arguments because it searches the input in self:

sage: Word('011').first_occurrence(w)
sage: Word('101').first_occurrence(w) is None

Or using find as in Python method find for str which returns -1 if not found:

sage: Word('011').find(w)
sage: Word('101').find(w)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2022-07-11 06:21:34 +0100

Seen: 117 times

Last updated: Jul 17 '22