Home>Article>Backend Development> Give an example to illustrate that spaces are characters in python
Are spaces considered characters in python?
The answer isYes, spaces are also characters in Python.
Case:
Enter a line of characters and count the number of English letters, spaces, numbers and other characters. number.
#!/usr/bin/python # -*- coding: UTF-8 -*- import string s = raw_input('input a string:\n') letters = 0 space = 0 digit = 0 others = 0 for c in s: if c.isalpha(): letters += 1 elif c.isspace(): space += 1 elif c.isdigit(): digit += 1 else: others += 1 print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,othePython
Method to determine whether characters belong to numbers, letters, or spaces:
Character c.isalpha()
Space c.isspace()
digit c.isdigit()
The above is the detailed content of Give an example to illustrate that spaces are characters in python. For more information, please follow other related articles on the PHP Chinese website!