Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Count [] chuỗi Python với sự trợ giúp của các ví dụ.
Phương thức
string.count[substring, start=..., end=...]1 trả về số lần xuất hiện của chuỗi con trong chuỗi đã cho.
Thí dụ
message = 'python is popular programming language'
# number of occurrence of 'p'
print['Number of occurrence of p:', message.count['p']]
# Output: Number of occurrence of p: 4
Cú pháp của số lượng chuỗi
Phương pháp cú pháp của phương pháp
string.count[substring, start=..., end=...]1 là:
string.count[substring, start=..., end=...]
Count [] tham số
Phương thức
string.count[substring, start=..., end=...]1 chỉ yêu cầu một tham số duy nhất để thực thi. Tuy nhiên, nó cũng có hai tham số tùy chọn:
- Chất nền - chuỗi có số lượng sẽ được tìm thấy. - string whose count is to be found.
- Bắt đầu [Tùy chọn] - Chỉ mục bắt đầu trong chuỗi nơi tìm kiếm bắt đầu. - starting index within the string where search starts.
- Kết thúc [Tùy chọn] - Chỉ mục kết thúc trong chuỗi nơi tìm kiếm kết thúc. - ending index within the string where search ends.
Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1. Index in Python starts from 0, not 1.
Đếm [] Giá trị trả về
Phương thức
string.count[substring, start=..., end=...]1 Trả về số lần xuất hiện của chuỗi con trong chuỗi đã cho.
Ví dụ 1: Số lượng xuất hiện của một chuỗi con đã cho
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
Đầu ra
The count is: 2
Ví dụ 2: Số lần xuất hiện của một chuỗi con nhất định bằng cách sử dụng bắt đầu và kết thúc
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
Đầu ra
The count is: 1
Ví dụ 2: Số lần xuất hiện của một chuỗi con nhất định bằng cách sử dụng bắt đầu và kết thúc
Ở đây, việc đếm bắt đầu sau khi
string.count[substring, start=..., end=...]5 đầu tiên gặp phải, tức là vị trí chỉ số
string.count[substring, start=..., end=...]6.
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2___& nbsp; Phương pháp #5: Sử dụng string.count[substring, start=..., end=...]
18 Naive method
Biểu thức thường xuyên có thể giúp chúng tôi đạt được nhiều nhiệm vụ mã hóa liên quan đến chuỗi. Họ cũng có thể tạo điều kiện cho chúng tôi đạt được nhiệm vụ tìm kiếm sự xuất hiện của phần tử trong chuỗi.
string.count[substring, start=..., end=...]9
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
1The count is: 14
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 The count is: 16
The count is: 24
The count is: 18
________ 62 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7Count of e in GeeksforGeeks is : 45
Phương pháp số 3: Sử dụng
Count of e in GeeksforGeeks is : 46
The count is: 26
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
1# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
2 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
3# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
4________ 45 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
8Đầu ra:
Count of e in GeeksforGeeks is : 4
Phương pháp số 2: Sử dụng string.count[substring, start=..., end=...]
1 Using
string.count[substring, start=..., end=...]1
Sử dụng
string.count[substring, start=..., end=...]1 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.
string.count[substring, start=..., end=...]9
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
1The count is: 14
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 The count is: 16
The count is: 24
The count is: 18
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
2 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
3# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
4________ 45 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
8Đầu ra:
Count of e in GeeksforGeeks is : 4
Phương pháp số 2: Sử dụng string.count[substring, start=..., end=...]
1 Using
Count of e in GeeksforGeeks is : 46
Sử dụng
string.count[substring, start=..., end=...]1 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.
Count of e in GeeksforGeeks is : 47
Count of e in GeeksforGeeks is : 48
Count of e in GeeksforGeeks is : 49
Count of e in GeeksforGeeks is : 40
string.count[substring, start=..., end=...]9
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
1# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 Count of e in GeeksforGeeks is : 46
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
2 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
3# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
4________ 80 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7Count of e in GeeksforGeeks is : 43
The count is: 24
Count of e in GeeksforGeeks is : 45
Đầu ra:
Count of e in GeeksforGeeks is : 4
Phương pháp số 4: Sử dụng Lambda + Count of e in GeeksforGeeks is : 4
6 + Count of e in GeeksforGeeks is : 4
7 Using lambda +
Count of e in GeeksforGeeks is : 46 +
Count of e in GeeksforGeeks is : 47
Các chức năng của Lambda, cùng với
Count of e in GeeksforGeeks is : 46 và ____ có thể đạt được nhiệm vụ cụ thể này là đếm tổng số lần xuất hiện của phần tử cụ thể trong một chuỗi. Điều này sử dụng ____90 để tổng hợp tất cả các lần xuất hiện thu được bằng cách sử dụng ____89.
string.count[substring, start=..., end=...]9
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
1# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 Count of e in GeeksforGeeks is : 46
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
2 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
3# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
4________ 80 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7Count of e in GeeksforGeeks is : 43
The count is: 24
Count of e in GeeksforGeeks is : 45
Đầu ra:
Count of e in GeeksforGeeks is : 4
Phương pháp số 4: Sử dụng Lambda +
Count of e in GeeksforGeeks is : 46 +
Count of e in GeeksforGeeks is : 47
Method #5 : Using
string.count[substring, start=..., end=...]18
Các chức năng của Lambda, cùng với
Count of e in GeeksforGeeks is : 46 và ____ có thể đạt được nhiệm vụ cụ thể này là đếm tổng số lần xuất hiện của phần tử cụ thể trong một chuỗi. Điều này sử dụng ____90 để tổng hợp tất cả các lần xuất hiện thu được bằng cách sử dụng ____89.
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
2___string.count[substring, start=..., end=...]9
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
0 # define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
1________ 45 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
8# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
2 # define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
3# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
4________ 80 ________ 40 & nbsp;
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
7Count of e in GeeksforGeeks is : 43
The count is: 24
Count of e in GeeksforGeeks is : 45
Đầu ra:
Count of e in GeeksforGeeks is : 4