파이썬 문자열 count() 메서드
예시
문자열에 "apple" 값이 나타나는 횟수를 반환합니다.
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
정의 및 사용
이 count()
메서드는 지정된 값이 문자열에 나타나는 횟수를 반환합니다.
통사론
string.count(value, start, end)
매개변수 값
Parameter | Description |
---|---|
value | Required. A String. The string to value to search for |
start | Optional. An Integer. The position to start the search. Default is 0 |
end | Optional. An Integer. The position to end the search. Default is the end of the string |
더 많은 예
예시
위치 10에서 24까지 검색:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)