How to check if a string ends with a question mark in Python

Method 1

s = "bro?"
if s[-1] == '?':
  print("yes")
else:
  print("no")

Method 2

s = "bro?"
if s.endswith("?"):
  print("yes")
else:
  print("no")

Method 3 - RegEx

return re.match(r"^.+([?])$", s)

Execution time test

import math
import timeit
import re

def is_string_ends_method_1(s):
    return s[-1] == "?"


def is_string_ends_method_2(s):
    return s.endswith("?")


def is_string_ends_method_3(s):
    return re.match(r"^.+([?])$", s)


def main():
    time = timeit.repeat(lambda: is_string_ends_method_1("bro?"))
    print("Method 1 time - ", time)
    time = timeit.repeat(lambda: is_string_ends_method_2("bro?"))
    print("Method 2 time - ", time)
    time = timeit.repeat(lambda: is_string_ends_method_3("bro?"))
    print("Method 3 time - ", time)


if __name__ == "__main__":
    main()

Result

Method 1 time -  [0.1822675, 0.17344950000000003, 0.1787824, 0.17679099999999992, 0.16371100000000005]
Method 2 time -  [0.28219320000000003, 0.2874809999999999, 0.2832559000000001, 0.2793295, 0.2779083]
Method 3 time -  [1.1234030000000002, 1.0968555999999996, 1.1038258, 1.1052483999999998, 1.1809824999999998]

You can see that method 1 is the fastest (s[-1] == "?" ).