Home>Article>Backend Development> Give an example to illustrate that spaces are characters in python

Give an example to illustrate that spaces are characters in python

anonymity
anonymity Original
2019-06-13 14:26:57 7857browse

Are spaces considered characters in python?

The answer isYes, spaces are also characters in Python.

Give an example to illustrate that spaces are 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Does python support win10? Next article:Does python support win10?