python 的字符串格式判断

来源:互联网 发布:王宝强剧情反转知乎 编辑:程序博客网 时间:2024/06/07 19:25

原文: http://www.cnblogs.com/cdinc/p/5945339.html

字符串格式的判断方法

# str为字符串    # str.isalnum() 所有字符都是数字或者字母    # str.isalpha() 所有字符都是字母    # str.isdigit() 所有字符都是数字    # str.islower() 所有字符都是小写    # str.isupper() 所有字符都是大写    # str.istitle() 所有单词都是首字母大写    # str.isspace() 所有字符都是空白字符、\t、\n、\r

python 判断字符串中是否只有中文字符

http://blog.csdn.net/hit0803107/article/details/52885702

在Python中字符串的表示是 用unicode编码。所以在做编码转换时,通常要以unicode作为中间编码。

decode的作用是将其他编码的字符串转换成unicode编码,比如 a.decode('utf-8'),表示将utf-8编码的字符串转换成unicode编码

encode的作用是将unicode编码的字符串转换成其他编码格式的字符串,比如b.encode('utf-8'),表示将unicode编码格式转换成utf-8编码格式的字符串


判断一个字符串中是否含有中文字符:

#-*- coding:utf-8 -*-import sysreload(sys)sys.setdefaultencoding('utf8')def check_contain_chinese(check_str):    for ch in check_str.decode('utf-8'):        if u'\u4e00' <= ch <= u'\u9fff':            return True    return False