新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何比較字符串
python可使用cmp()方法來比較兩個對象,相等返回 0 ,前大于后,返回 1,小于返回 -1。

a = "abc" b = "abc" c = "aba" d = "abd" print cmp(a,b) print cmp(a,c) print cmp(a,d) //返回 0 1 -1
相關推薦:《Python視頻教程》
Python3.X 的版本中已經(jīng)沒有cmp函數(shù),如果你需要實現(xiàn)比較功能,需要引入operator模塊,適合任何對象,包含的方法有:
operator.lt(a, b) operator.le(a, b) operator.eq(a, b) operator.ne(a, b) operator.ge(a, b) operator.gt(a, b) operator.__lt__(a, b) operator.__le__(a, b) operator.__eq__(a, b) operator.__ne__(a, b) operator.__ge__(a, b) operator.__gt__(a, b)
實例
>>> import operator
>>> operator.eq('hello', 'name');
False
>>> operator.eq('hello', 'hello');
True
注意:python3中使用==可進行比較兩個字符串,與java中的==代表相等的含義不同。
新聞名稱:創(chuàng)新互聯(lián)Python教程:python如何比較字符串
網(wǎng)站URL:http://www.5511xx.com/article/coepseg.html


咨詢
建站咨詢
