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

Give an example to illustrate that spaces are characters in python

anonymity
Release: 2019-06-14 09:54:57
Original
7978 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template