The .split
or .rsplit
method is one solution that can get you there:
s = """\
Your One-time Verification Code is:
366522\
"""
# Split on newline, and pass limit=1 so we stop after N=1 splits. Note that
# the split is performed from the end of the string, since we use rsplit.
print(s.rsplit('\n', 1)[-1])
# 366522
If you have more lines after the verification code, use .split
instead:
s = """\
Your One-time Verification Code is:
366522
Testing, testing
1 2 3
"""
# Split on newline, and pass limit=3 so we stop after N=3 splits. Then grab
# the third element (the last line that we split)
print(s.split('\n', 3)[2])
# 366522