轻松python文本专题-判断对象里面是否是类字符串(推荐使用isinstance(obj,str))

场景:

判断对象里面是否是类字符串

一般立刻会想到使用type()来实现

>>> def isExactlyAString(obj):
	return type(obj) is type('')

>>> isExactlyAString(1)
False
>>> isExactlyAString('1')
True
>>> 

还有

>>> def isAString(obj):
	try :obj+''
	except:return False
	else:return True

>>> isAString(1)
False
>>> isAString('1')
True
>>> isAString({1})
False
>>> isAString(['1'])
False
>>> 

虽然思路上和方法使用上都没用问题,但是如果从python的特性出发,我们可以找到更好的方法:isinstance(obj,str)

>>> def isAString(obj):
	return isinstance(obj,str)

>>> isAString(1)
False
>>> isAString('1')
True
>>> 

str作为python3里面唯一的一个字符串类,我们可以检测字符串是否是str的实例

就说到这里,谢谢大家

------------------------------------------------------------------

点击跳转零基础学python-目录

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 16:34:17

轻松python文本专题-判断对象里面是否是类字符串(推荐使用isinstance(obj,str))的相关文章

python文本 判断对象里面是否是类字符串

场景: 判断对象里面是否是类字符串 一般立刻会想到使用type()来实现 >>> def isExactlyAString(obj):        return type(obj) is type('')        >>> isExactlyAString(1)    False    >>> isExactlyAString('1')    True    >>> 还有 >>> def isAString(o

轻松python文本专题-字符串开头或者结尾匹配

场景: 字符串开头或者结尾匹配,一般是使用在匹配文件类型或者url 一般使用startwith或者endwith >>> a='http://blog.csdn.net/raylee2007' >>> a.startswith ('http') True 注意:这两个方法里面的参数可以是str,也可以是元组,但是不可以是列表和字典 >>> a='http://blog.csdn.net/raylee2007' >>> a.starts

轻松python文本专题-字符串对齐

场景: 字符串对齐 python提供非常容易的方法,使得字符串对齐 >>> print("abc".center (30,'-')) -------------abc-------------- >>> print("abc".ljust (30)+'|') abc | >>> print("abc".rjust (30)) abc >>> 分别是center,ljust,r

轻松python文本专题-去掉字符串前后空格

场景: 去掉字符串前后空格 可以使用strip,lstrip,rstrip方法 >>> a="abc".center (30) >>> a ' abc ' >>> b=a.lstrip () >>> b 'abc ' >>> c=a.rstrip () >>> c ' abc' >>> d=a.strip () >>> d 'abc' >

轻松python文本专题-maketrans和translate

场景: 过滤字符串的某些字符,我们从例子出发 >>> tb=str.maketrans ('abc','123') >>> 'abcd'.translate (tb) '123d' >>> 'abcd+++a+b+cd'.translate (tb) '123d+++1+2+3d' >>> 1.建立字符映射表,也就是maketrans方法所做的事情,它返回一个字符串的映射表,意思是:如果字符串里面出现a,那么它就会变成对应的1,如此类

判断对象是否是某个类的实例

判断对象是否是某个类的实例,可以用instanceof运算符,但是不推荐使用 比如var obj = new Date(); obj instanceof Date;//true obj instanceof Object;//true obj instanceof Array;//true 推荐使用constructor属性判断. obj.constructor == Date //true function Test(){} var tt = new Test(); tt.construct

javascript 判断对象是否为空,字符串是否为空

//判断对象是否为空 //console.log(isEmptyObject());           //true //console.log(isEmptyObject({}));         //true //console.log(isEmptyObject(null));       //true //console.log(isEmptyObject(23));         //true //console.log(isEmptyObject({"te": 2})

Python 代码优化基础——判断对象类型

# -*- coding: utf-8 -*- # # def displayNumType(num): # print num, 'is', # if type(num) == type(0): # print 'an integer' # elif type(num) == type(0L): # print 'a long' # elif type(num) == type(0.0): # print 'a float' # elif type(num) == type(0+0j): #

java 判断对象是否是某个类的类型方法

class Do1 { public static void main(String[] args) { AA a=new CC(); if(a instanceof CC) { CC b=(CC)a; b.run(); } } } class AA { int y=3; void brun() { System.out.println(y); } } class CC extends AA { int x=1; void run() { System.out.println(x); } } c