Python - 判断字符串和类字符串
来源:互联网 发布:淘宝代办出生公证 编辑:程序博客网 时间:2024/06/14 11:22
判断字符串和类字符串
本文地址: http://blog.csdn.net/caroline_wendy/article/details/20461879
Python中, 测试一个字符串是否是类字符串, 有两种方法:
方法1:
可以判断是否是basestring的一个示例;
因为basestring是str和unicode的超类(superclass), 即This abstract type is the superclass for str and unicode;
但却无法检测UserString模块提供的类的实例.
方法2:
可以使用简单的测试, 判断其是否符合字符串的规则, 即 str+''是否是一个字符串;
代码如下:
# -*- coding: utf-8 -*-#====================#File: TextExercise.py#Author: Wendy#Date: 2014-03-02#====================#eclipse pydev, python2.7import UserStringdef isAString (anobj): return isinstance(anobj, basestring)def isStringLike (anobj): try: anobj + '' + anobj.lower() except: return False else: return True str = UserString.UserString('Girl')print(str),print(isAString(str)), #无法检测UserString类print(isStringLike(str))print('')str2 = 'Girl-Boy'print(str2),print(isAString(str2)), #无法检测UserString类print(isStringLike(str2))
输出:
Girl False TrueGirl-Boy True True
5 0
- Python - 判断字符串和类字符串
- Python判断文件和字符串编码类型
- python 判断字符串方法
- python判断字符串
- python判断字符串
- python 判断字符串方法
- 【python】判断字符串
- 【python】判断字符串类型
- python字符串编码判断
- python判断字符串
- python 判断字符串
- python判断字符串
- 基于python的数字判断程序和字符串判断程序
- python 判断字符串是否包含子字符串
- 整型和字符串判断
- python 对字符串的判断
- python 判断字符串是否空
- Python判断Unicode字符串类型
- 怎么写项目解决方案
- 一周 项目3 日期结构体
- WebView中使用HTML打开本地应用
- JiaThis加网--分享按钮,稳步提升您的网站流量。
- 建堆的时间复杂度计算
- Python - 判断字符串和类字符串
- c语言内存分配函数之间的区别
- Linux下不同颜色的文件类型和Linux下用字符表示的文件类型
- ubuntu查看占用某端口的程序
- 俞军十二条
- Linux Shell编程入门
- Warning: mysqli::mysqli(): (HY000/2002): No connection could be made because the target machine acti
- JQuery中$.ajax()方法参数详解
- 演进分组系统的几个概念 EPS/EPC/SAE/LTE/EUTRAN