Home  >  Article  >  Backend Development  >  Introduction to Python's judgment of whether a parameter is a legal identifier

Introduction to Python's judgment of whether a parameter is a legal identifier

高洛峰
高洛峰Original
2017-03-22 10:50:372658browse

This article explains in detail about Python's introduction to determining whether a parameter is a legal identifier

import string

def is_valid_identifier(param):
    alphas = string.letters + '_'
    nums = string.digits

    if len(param) > 1:
        if param[0] not in alphas:
            print 'invalid:first symbol must be alphabetic'
        else:
            for otherChar in param[1:]:
                if otherChar not in alphas + nums:
                    print 'invalid:reminding symbols must be alphanumeric'
                    break
            else:
                 print 'okay, %s is an valid identifier'%param


is_valid_identifier('class')

The above is the detailed content of Introduction to Python's judgment of whether a parameter is a legal identifier. 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