Python String count() Metode
Contoh
Kembalikan berapa kali nilai "apel" muncul di string:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definisi dan Penggunaan
Metode count()
mengembalikan berapa kali nilai tertentu muncul dalam string.
Sintaksis
string.count(value, start, end)
Nilai Parameter
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 |
Lebih Banyak Contoh
Contoh
Cari dari posisi 10 hingga 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)