산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> python

예제가 있는 Python 문자열 count()

파이썬 수

count()는 파이썬의 내장 함수입니다. 문자열에서 주어진 요소의 총 개수를 반환합니다. 카운팅은 문자열의 처음부터 끝까지 시작됩니다. 검색을 시작하려는 위치에서 시작 및 끝 인덱스를 지정할 수도 있습니다.

이 Python 자습서에서는 다음을 배우게 됩니다.

PythonString Count() 구문

파이썬 카운트 함수 구문:

string.count(char or substring, start, end)

파이썬 구문의 매개변수

반환값

count() 메서드는 정수 값, 즉 주어진 문자열에서 주어진 요소의 개수를 반환합니다. 주어진 문자열에 값이 없으면 0을 반환합니다.

예시 1:문자열에 대한 카운트 메소드

다음 예제는 문자열에 대한 count() 함수의 작동을 보여줍니다.

str1 = "Hello World"
str_count1 = str1.count('o')  # counting the character “o” in the givenstring
print("The count of 'o' is", str_count1)

str_count2 = str1.count('o', 0,5)
print("The count of 'o' usingstart/end is", str_count2)

출력:

The count of 'o' is 2
The count of 'o' usingstart/end is 1

예시 2:주어진 문자열에서 문자의 발생 횟수 계산

다음 예는 시작/종료 인덱스를 사용하여 주어진 문자열과 에서 문자의 발생을 보여줍니다.

str1 = "Welcome to Guru99 Tutorials!"
str_count1 = str1.count('u')  # counting the character “u” in the given string
print("The count of 'u' is", str_count1)

str_count2 = str1.count('u', 6,15)
print("The count of 'u' usingstart/end is", str_count2)

출력:

The count of 'u' is 3
The count of 'u' usingstart/end is 2

예시 3:주어진 문자열에서 부분 문자열의 발생 횟수 계산

다음 예제는 start/endindex를 사용할 뿐만 아니라 주어진 문자열에서 부분 문자열의 발생을 보여줍니다.

str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses"
str_count1 = str1.count('to') # counting the substring “to” in the givenstring
print("The count of 'to' is", str_count1)
str_count2 = str1.count('to', 6,15)
print("The count of 'to' usingstart/end is", str_count2)

출력:

The count of 'to' is 2
The count of 'to' usingstart/end is 1

요약:


python

  1. 하위 문자열 및 예제가 있는 Java 문자열 indexOf() 메서드
  2. EXAMPLE이 있는 Python String strip() 함수
  3. Python String format() 예제로 설명
  4. 예제가 있는 Python 문자열 find() 메서드
  5. 예제가 있는 Python Lambda 함수
  6. 예제가 있는 Python round() 함수
  7. 예제가 있는 Python map() 함수
  8. 예제가 있는 Python Timeit()
  9. 예제가 있는 컬렉션의 Python 카운터
  10. 예제가 있는 Python의 type() 및 isinstance()