TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | count() method with Examples

Python String count() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String Count() Method

It returns the number of occurences of substring in the specified range. It takes three parameters, first is a substring, second a start index and third is last index of the range. Start and end both are optional whereas substring is required.

Signature

count(sub[, start[, end]])

Parameters

  • sub (required)
  • start (optional)
  • end (optional)

Return Type

It returns number of occurrences of substring in the range.

Let's see some examples to understand the count() method.

Python String Count() Method Example 1

# Python count() function example
# Variable declaration
str = "Hello TheDeveloperBlog"
str2 = str.count('t')
# Displaying result
print("occurences:", str2)

Output:

occurences: 2

Python String Count() Method Example 2

# Python count() function example
# Variable declaration
str = "ab bc ca de ed ad da ab bc ca"
oc = str.count('a')
# Displaying result
print("occurences:", oc)

Here, we are passing second parameter (start index).

Python String Count() Method Example 3

# Python count() function example
# Variable declaration
str = "ab bc ca de ed ad da ab bc ca"
oc = str.count('a', 3)
# Displaying result
print("occurences:", oc)

Output:

occurences: 5

The below example is using all three parameters and returning result from the specified range.

Python String Count() Method Example 4

# Python count() function example
# Variable declaration
str = "ab bc ca de ed ad da ab bc ca"
oc = str.count('a', 3, 8)
# Displaying result
print("occurences:", oc)

Output:

occurences: 1

It can count non-alphabet chars also, see the below example.

Python String Count() Method Example 5

# Python count() function example
# Variable declaration
str = "ab bc ca de ed ad da ab bc ca 12 23 35 62"
oc = str.count('2')
# Displaying result
print("occurences:", oc)

Output:

occurences: 3

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf