使用python实现strcmp函数功能示例

yipeiwu_com6年前Python基础

实现这个功能我相信大家一定明白他的意思了,很简单了,下面的代码大家参考使用吧

复制代码 代码如下:

def strcmp(str1,str2):
        i = 0
        while i<len(str1) and i<len(str2):
                outcome = cmp(str1[i],str2[i])
                if outcome:
                        print outcome
                        return outcome
                i +=1
        return cmp(len(str1),len(str2))
str1='dfdcd'
str2='dfdc'

print strcmp(str1,str2)
print cmp(str1,str2)

相关文章

python里使用正则的findall函数的实例详解

python里使用正则的findall函数的实例详解 在前面学习了正则的search()函数,这个函数可以找到一个匹配的字符串返回,但是想找到所有匹配的字符串返回,怎么办呢?其实得使用f...

python分析apache访问日志脚本分享

#!/usr/bin/env python # coding=utf-8 #---------------------------------------------------...

python3利用ctypes传入一个字符串类型的列表方法

c语言里:c_p.c #include <stdio.h> void get_str_list(int n, char *b[2]) { printf("in c s...

python2.7使用plotly绘制本地散点图和折线图

python2.7使用plotly绘制本地散点图和折线图

本人在学习使用Python和plotly处理数据时,经过两个小时艰难试错,终于完成了散点图和折线图的实例。在使用过程中遇到一个大坑,因为官方给出的案例是用在线存储的,所以需要安装jupy...

CentOS7.3编译安装Python3.6.2的方法

CentOS7.3编译安装Python3.6.2的方法

我使用的是 CentOS7.3 安装 Python3.6.2 1.查看是否已经安装Python Centos7 默认安装了Python2.7.5 因为一些命令要用它比如 yum 它使用...