[파이썬, Python] String 검색

count(keyword, [start, [end]]) - keyword 포함 개수
endswith(postfix, [start, [end]]) - 문자열 끝에 postfix가 있는지 검사
find(keyword, [start, [end]]) - keyword가 있는 첫번째 인덱스, 없으면 -1
index(keyword, [start, [end]]) - keyword가 있는 첫번째 인덱스, 없으면 Error 발생


[출처] Python string|작성자 대한민군


>>> 'python'.count('p')

1

>>> 'python'.count('p',1)

0

>>> 'python'.find('p')

0

>>> 'python'.find('i')

-1

>>> 'python'.index('p')

0

>>> 'python'.index('i')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: substring not found

>>> 'python'.endswith('on')

True

>>> 'python'.endswith('hon')

True

>>> 'python'.endswith('onn')

False


반응형

+ Recent posts