If the word order is one followed by three, you can extract it using the regular expression below.
import re
s="one two four five
one two four
one four four five
"""one two five"""
s2 = '\n'.join(re.findall(r'^.*one.*three.*$', s, re.MULTILINE))
print(s2)
If you want to use splitlines()
to make a list of strings by line.
s="one two four four five
one two four
one four four five
"""one two five"""
For looping with a for statement, use the following code:
result='"
for line in s.splitlines (True):
if 'one' in line and 'three' in line:
result+=line
You can also use the expression to write as follows:
result='.join(x for x in s.splitlines(True) if 'one' in x and 'three' in x )
In addition, splitlines(True)
lists newline characters, so you can process them without considering the model dependence of newline characters.
© 2025 OneMinuteCode. All rights reserved.