Python 3 make the program find out how much of a given digit is in a string or a number

Asked

Viewed 2,297 times

4

It’s for python 3:

I’m trying to make a very basic program to find out how much of a given digit is in a string or a number.

For example, if the user writes 1200, I want to print-"your number has 2 zeros".... or if the user writes "hi, all right?" i want to print-"your phrase has 2’s

  • Hello Matthew, Welcome to Stackoverflow PT! Try to explain your question better with some code you already have, and detail a little more. You want the highest occurrence character in a string or want all?

3 answers

3


See working on repl
Code in the github

# coding: utf-8

# Informe uma frase
frase = input("Informe uma frase: ")
# Informe qual o caractere que deseja saber a quantidade contida na frase
caracter = input("Informe o caracter que deseja contar: ")
# método: count - Conta quantas vezes o caractere foi usado na frase
# Imprime a quantidade.
print("Sua frase tem: " + str(frase.count(caracter)) + " digito(s): " + str(caracter))

Source: original question on English

  • Thank you, the method worked for strings. But for numbers, the program says that the function . Count does not accept integers, it can solve this?

  • Now it works, I had forgotten that detail

  • It’s right that it’s working, the link you put up looks Naum

  • You have to click the button player or use the shortcut CRTL+ENTER ai in the painel to your right will rotate the code.

  • See I think you don’t understand what is being asked: "if the user writes 1200, I want to print-"your number has 2 zeros"", ie the output either number or string should show how many x repeat, you understand?

  • Yes is exactly what the code does, it asks the user to inform a sentence, then it asks you to inform the character you wish to count.

  • Um understood now your approach... You’re correct, my fault

  • It worked, thank you very much

Show 3 more comments

3

Extending the answer of @Wéllingthon M. de Souza who answered and well to your question, here are two alternatives in case you want to count all characters:

from collections import Counter

frase = input("Informe uma frase: ")
count_chars = Counter(frase)

text = '\n'.join('A Frase tem {} {}'.format(v, k) for k, v in count_chars.items())
print(text)

DEMONSTRATION

In a more didactic way it can be:

frase = input("Informe uma frase: ")

count_chars = {}
for char in frase:
    if char not in count_chars:
        count_chars[char] = 0
    count_chars[char] += 1

for char in count_chars:
    print('A Frase tem {} {}'.format(count_chars[char], char))

DEMONSTRATION

0

One of the ways to resolve this issue is by using List Comprehensions. For this you can use the following logic:

  1. Define a variable in which the captured string will be stored;
  2. Define a variable in which the captured character to count will be stored;
  3. Implement a comrehension list;
  4. Display the result.

With this logic you can implement the following code:

frase = input('Digite uma frase: ')
caract = input('Digite o carácter a ser contado: ')
cont = sum([1 for i in frase if i == caract])

print(f'Na frase existe {cont} caracteres "{caract}".')

How this code works?

Initially this code captures a string - string - and, also, a character we want to count. Later, a list will be mounted by list comprehension. This list will be mounted with only values 1 if, and only if, the temporary variable "i = caract". Then the function sum() shall sum up the amount of 1 existing grandlist. Finally the amount of character analyzed will be displayed.

Testing the code:

Imagine that when we executed this code we typed the following sentence:

diclorodifeniltricloroetano foi o um dos primeiros pesticidas modernos

and then specify the character "the" to be counted.

The exit code will be:

Na frase existe 11 caracteres "o".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.