Counting occurence within a string Python

ddbg

I am writing a code to count the occurrences of each letter within a string. I understand that it has been asked and answered Count occurrence of a character in a string, however I cannot figure out why it will not count when I use it.

def percentLetters(string):
  string = 'A','B'
  print string.count('A')
  print string.count('B')

If I was to input percentLetters('AABB'), I would anticipate receiving A=2 and B=2 but am having no such luck. I tried using an if statement earlier however it would not print anything at all

def percentLetters(string):
string='A','B'
if 'A':
  print string.count('A')
if 'B':
  print string.count('B')

This doesn't work either. Anyone who might have some insight it would be helpful

Padraic Cunningham

Don't reassign string inside the function and better to not use string as a variable name at all.

def percentLetters(s):
        print s.count('A')
        print s.count('B')
percentLetters('AABB')
2
2

string = 'A','B' means you set the string variable to a tuple containing just ("A","B"), it is not pointing to the string you pass in.

In [19]: string = 'A','B'

In [20]: string
Out[20]: ('A', 'B')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP: Counting Extensions Occurence on a String

From Dev

Counting the occurence of a specific string in a csv file

From Dev

Counting items occurence in a list - the python way

From Dev

Counting items occurence in a list - the python way

From Dev

counting occurence of a word in a text file using python

From Dev

Python extract occurence of a string with regex

From Dev

Counting occurence of a table of values

From Dev

Counting the occurence of substrings in matlab

From Dev

Counting the occurence of substrings in matlab

From Dev

Counting occurence of a table of values

From Dev

find last occurence of multiple characters in a string in Python

From Dev

Occurence of a multiple symbol in a string-Python

From Dev

Counting words in a String in Python

From Dev

Counting the number of occurrences of a substring within a string in PostgreSQL

From Dev

Counting occurrences of a character in a string within a cell

From Dev

Counting occurence with a condition using awk

From Dev

Counting in R and preserving the order of occurence

From Dev

Python: Counting occurrence of List element within List

From Dev

Python: Counting occurrences of List element within List

From Dev

Counting occurence based on condition for each element of a column

From Dev

Counting occurence of a word in a text file using R

From Dev

Javascript on counting occurence of letter 'Z' in for loop textfield

From Dev

Counting co-occurence of strings in lists of lists

From Dev

Counting cumulative unique occurence in a R data frame

From Dev

Counting occurence of number in list upon user input

From Dev

Counting first occurence of consecutive values that pass criteria

From Dev

Using Regex_substr in Oracle to select string up to the last occurence of a space within \n characters length

From Dev

Bash: return all the characters between the nth occurence of two different strings within a string

From Dev

Counting all occurrences of a string within all files in a folder

Related Related

  1. 1

    PHP: Counting Extensions Occurence on a String

  2. 2

    Counting the occurence of a specific string in a csv file

  3. 3

    Counting items occurence in a list - the python way

  4. 4

    Counting items occurence in a list - the python way

  5. 5

    counting occurence of a word in a text file using python

  6. 6

    Python extract occurence of a string with regex

  7. 7

    Counting occurence of a table of values

  8. 8

    Counting the occurence of substrings in matlab

  9. 9

    Counting the occurence of substrings in matlab

  10. 10

    Counting occurence of a table of values

  11. 11

    find last occurence of multiple characters in a string in Python

  12. 12

    Occurence of a multiple symbol in a string-Python

  13. 13

    Counting words in a String in Python

  14. 14

    Counting the number of occurrences of a substring within a string in PostgreSQL

  15. 15

    Counting occurrences of a character in a string within a cell

  16. 16

    Counting occurence with a condition using awk

  17. 17

    Counting in R and preserving the order of occurence

  18. 18

    Python: Counting occurrence of List element within List

  19. 19

    Python: Counting occurrences of List element within List

  20. 20

    Counting occurence based on condition for each element of a column

  21. 21

    Counting occurence of a word in a text file using R

  22. 22

    Javascript on counting occurence of letter 'Z' in for loop textfield

  23. 23

    Counting co-occurence of strings in lists of lists

  24. 24

    Counting cumulative unique occurence in a R data frame

  25. 25

    Counting occurence of number in list upon user input

  26. 26

    Counting first occurence of consecutive values that pass criteria

  27. 27

    Using Regex_substr in Oracle to select string up to the last occurence of a space within \n characters length

  28. 28

    Bash: return all the characters between the nth occurence of two different strings within a string

  29. 29

    Counting all occurrences of a string within all files in a folder

HotTag

Archive