473,413 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

how to count letters only in a string

hello,
I was stuck on a way to count the number of alphabetic characters in an input string, as well as a way to count the number of numeric characters. I've been toying with the isalpha and isdigit string methods, but can't figure anything out. So far I have:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     sentence = input('Enter a sentence: ')
  3.     Stats(sentence)
  4.  
  5. def Stats(input):
  6.     print('Total characters:', character(input))
  7.     print('Letters:', letter(input))
  8.     print('Digits:', digit(input))
  9.  
  10. def character(input):
  11.     count = 0
  12.     for char in input:
  13.         count += 1
  14.     return (count)
  15.  
  16. def letter(input):
  17. # This is where I'm stuck.  Not sure how to use isalpha() 
  18. # to make this work
  19.  
  20. def digit(input):
  21. # here as well, but with the isdigit().
Nov 1 '11 #1
3 34806
Glenton
391 Expert 256MB
Hi. It makes it a bit easier if you use the code tags (# button).

The structure of your code if very good and clear though.

Here's how you could adapt character to make letter
Expand|Select|Wrap|Line Numbers
  1. def letter(input)
  2.     count=0
  3.     for char in input:
  4.         if isalpha(char):
  5.             count += 1
  6.     return count
Good luck!

Incidendally, I don't think isalpha is defined in plane python, so perhaps you have defined it separately or imported something? If not, an isalpha function might look like this:

Expand|Select|Wrap|Line Numbers
  1. def isalpha(x):
  2.     return x in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Nov 1 '11 #2
bvdet
2,851 Expert Mod 2GB
isalpha() and isdigit() are string methods and return boolean values.

Expand|Select|Wrap|Line Numbers
  1. >>> "a".isalpha()
  2. True
  3. >>> "1".isdigit()
  4. True
  5. >>> "1".isalpha()
  6. False
  7. >>> 
Nov 1 '11 #3
Glenton
391 Expert 256MB
Oh, yes. LOL. I told you I'm not doing enough python
Nov 2 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Johannes Nix | last post by:
Hello, yesterday I met a cute person (after my dance class) who told me about an interesting experiment regarding cognition. People were told to read a typed text; However, in every word in the...
1
by: steve smith | last post by:
Hi i just wanted to knw the quickest way, of obtaining a count of the number of words in a string? Also how could I then output these words to a file, with only one word on each line in file, in...
3
by: Raed Sawalha | last post by:
I have the following letters; string letters = "a;b;c....to z"; the I need to replace the incoming string which containing letters above with integer 1 i did following for(int...
7
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes,...
1
by: willie | last post by:
>willie wrote: wrote:
4
by: gihan99 | last post by:
hey can somebody please explane how to count letters in a string. thxs
4
by: abueno | last post by:
//It should count how many characters are letters in the English alphabet, and is displaying the correct letters, but is not counting good. void FunctionCountLetters(char s) { int len; int i;...
2
by: DexterID | last post by:
Hi, I tried so much working with this to get distinct Words and their count. I'm using ASP.NET 2.0. And i also googled so much. I need HELP from anyone to get the required output. ...
6
by: TimSama | last post by:
The below code is to make a sentence upper case, and also get every first letter of every word in it, and put it in a different string to be called by the main function. When I try and call the...
2
by: nervusvagus | last post by:
Following is a code that counts the number of letters in a text file named "alice_in_wonderland.txt" So far I only have text: aaaaa bbbb cccc in the text file: # countletters.py def...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.