Python の startswith 関数で文字列の先頭一致を調べる2020年04月09日 12時41分56秒

Python で文字列が特定の文字列から始まっているかを調べる。startswith 関数が Python にある。
#!/usr/local/bin/python

text = "This is a pen."
        
print( text.startswith( 'This' ) )
print( text.startswith( 'is', 2, 4 ) )
また、オフセットを指定することで、中間地点の検査も出来る。
% ./str_startswith.py 
True
True