Use a regular expression to count the number of digits, non-digit characters, whitespace characters and words in a string.
What will be an ideal response?
```
# Count number of digits, characters, white space characters
# and words in a string.
import re
testString = raw_input( "Please input a string: " )
# split the string by digits, creating digits + 1 pieces
digits = len( re.split( "\d", testString ) ) - 1
if digits > 1:
print "There are %d digits in this string." % digits
elif digits == 1:
print "There is one digit in this string."
else:
print "There is no digit in this string."
characters = len( re.split( "\w", testString ) ) - 1
if characters > 1:
print "There are %d characters in this string." % \
characters
elif characters == 1:
print "There is one character in this string."
else:
print "These is no character in this string."
whitespaces = len( re.split( "\s", testString ) ) - 1
if whitespaces > 1:
print "There are %d white spaces in this string." \
% whitespaces
elif whitespaces == 1:
print "There is one white space in this string."
else:
print "There is no whitespace in this string."
words = len ( re.split( "\w+", testString ) ) - 1
if words > 1:
print "There are %d words in this string." % words
elif words == 1:
print "There is one word in this string."
else:
print "There is no word in this string."
```
Please input a string: Eight quick foxes jumped over 8 fences and 8 lazy
dogs.
There are 2 digits in this string.
There are 53 characters in this string.
There are 10 white spaces in this string.
There are 11 words in this string.
You might also like to view...
Standard code libraries in Java are called:
a. Methods b. Classes c. Packages d. Statements
Yellow has the triplet ____.
A. (0, 0, 0) B. (255, 255, 0) C. (0, 255, 255) D. (255, 0, 255)
What are the four kinds of prototyping?
What will be an ideal response?
A Web-based e-mail account that you can discontinue if your account begins to accumulate lots of spam is called a(n) ____.
A. disposable e-mail address B. temporary e-mail address C. e-mail address D. forwarded e-mail address