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
anonymityOriginal
2019-06-13 14:26:577911browse

Are spaces considered characters in python?

The answer is Yes, 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