分享

Python

 小区木大爷 2019-05-14

判断字符是否以某个字符开头和结尾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
# !/usr/bin/python3
__author__ = 'beimenchuixue'
__blog__ = 'http://www.cnblogs.com/2bjiujiu/'
def check_str(value):
    # 检查你输入的是否是字符类型
    if isinstance(value, str):
        # 判断字符串以什么结尾
        if value.endswith('.sh'):
            return '%s 是以.sh结尾的字符串' % value
        # 判断字符串以什么开头
        elif value.startswith('xi'):
            return '%s 是以xi开头的字符串' % value
        else:
            return '%s 不满足以上条件的字符串' % value
    else:
        return '%s is not str' % value
     
def main():
    str_one = 'bei_men.sh'
    resp_one = check_str(str_one)
    print(resp_one)
     
    str_two = 'xi_du.py'
    resp_two = check_str(str_two)
    print(resp_two)
     
    str_three = 233
    resp_three = check_str(str_three)
    print(resp_three)
if __name__ == '__main__':
    main()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多