Python detection variable name

Source: Internet
Author: User

Python detection variable name
    变量在编程中的用途非常广,在python中,变量的名称只能以字母或者下划线“_”开头,变量名只能由字母、数字、下划线组成。    编写python,使得其实现以下功能:    1、输入一个变量名;    2、当变量名首位不是字母或者下划线时,自动输出“该变量名不合适!”;    3、当变量名中有除了字母、数字和下划线以外的其他符号时,自动输出“该变量名不合适!”;    4、当变量名使用规范时,输出“该变量名可用”。    python的脚本如下:
Mport stringvariable = Raw_input ("Please input your variables:") Long_num = Len (Variable)If variable[0]NotIn String.letters +"_" :Print "This variable is unsuitable!" else: for i in range (1,long_num): Al_var = variable[i] if al_var not Span class= "Hljs-keyword" >in string.letters + string.digits +  "_": print  "This variable is unsuitable!" break else: pass while i = = long_num-1: print " This variable is available! " breakprint  "\ n"    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    该脚本运行后的如下所示:

    输入变量名为“123hello”时,输出“变量名不合适!”    输入变量名为“_hello”时,输出“变量名可以使用!”    输入变量名为“hello1/2”时,输出“变量名不合适!”    输入变量名为“hello 123”时,输出“变量名不合适!”

Python detection variable name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.