Original title:
Enter a line of characters, respectively, the statistics of the number of letters, spaces, numbers and other characters in them.
(This question does not support Chinese characters and Chinese characters)
My code:
# ! / Usr / bin / Python # encoding = UTF. 8- # - * - Coding: UTF-. 8 - * - # input line of characters, respectively, the statistics of the number of English letters, spaces, numbers and other characters therein. A = INPUT ( " Please INPUT String: \ n- " ) Space_ = 0 number_ = [] EnglishLetter = [] otherString = [] for I in A: IF I == " " : # spaces special, first determine the space space_ = + 1'd Space_ elif ( " 0 " <= I <= " . 9 " ): number_.append (I) elif ( " A " <= I <= " Z " ) or ( " A " <= I <= " the Z " ): EnglishLetter.append (I) the else : otherString. the append (I) # ". 1" and "a" are comparable; ";" and "a" are comparable; Print (Space_, number_, EnglishLetter, otherString) Print (Space_, len (Number _), len ( EnglishLetter), len (otherString))
Output:
May identify "\ t"
Because I use the built-in python IDLE, does not support the input string with a carriage return, but if the characters included directly \ n or \ t, will treat as two characters; if you manually enter "" \ t , it can be recognized;
Original title library using a string, as follows:
In fact, I think the next, this question there is another solution, for comparison against the ASCII table,
Or directly converting all characters to ASCII code corresponding to the value of
Or to establish a list of numbers, strings, lists, lists and other space character list;
If you want to consider the escape character can be judged whether the character is "\", and if so, to see what the next character is; this time to get to know all the escape characters in Python;
In short, this question should still concerned about the escape character, and familiar with the String library;
About recognize Chinese characters, and subsequent re-consider it;
-------- (I am dividing line) --------
reference:
1. RUNOOB.COM:https://www.runoob.com/python/python-exercise-example17.html
Remarks:
Initial modified: September 30, 2019 16:24:16
Environment: Windows 7 / Python 3.7.2
The original title given by way of answer: