count occurrences of character in string python code example

Example 1: python return number of characters in string

# Example usage:
your_string = "Example usage"
len(your_string)
--> 13

Example 2: string count substring occurences pytohn

string.count(substring, [start_index], [end_index])

Example 3: python count character occurrences

str1.count("a")

Example 4: how to count the occurrence of a word in string python

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count
print("The count is:", count)

Example 5: count number of occurrences of character in string

public class CountStringOccurence {
public static void main(String[] args) {

int count= countOccurences("aaassssddadad",'s');
System.out.println(count);

}

private static int countOccurences(String word, char character){
  
  int count = 0;
  for(int i = 0; i < word.length(); i++){
    
    if(word.chartAt(i) == character){
      
      count++; } 
                 }
      return count;
}
}

Example 6: check how many letters in a string python

#use the built in function len()

len("hello")

#or you can count the characters in a string variable

a = "word"
len(a)