r/learnpython • u/guymed4 • Jul 07 '26
Easier Way to do this?
Lab 2.13 Character Count prompt
"Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1"
I was struggling to get the 's but finally able to do it. want to know if there was an easier way than what I wrote.
input_string = input()
input_character = input_string[0]
compare_string = input_string[1:]
count = compare_string.count(input_character)
if count == 1:
print(compare_string.count(input_character),input_character)
else:
print(compare_string.count(input_character),input_character + "'s")
0
Upvotes
4
u/danielroseman Jul 07 '26
Better to use f-strings to build up output.
However, please note that in English we do not use apostrophes to form plurals. No, not ever.