使用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)

相关文章

机器学习之KNN算法原理及Python实现方法详解

本文实例讲述了机器学习之KNN算法原理及Python实现方法。分享给大家供大家参考,具体如下: 文中代码出自《机器学习实战》CH02,可参考本站: 机器学习实战 (Peter Harri...

python BeautifulSoup使用方法详解

直接看例子:复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*-from bs4 import BeautifulSouphtml_doc...

整理Python 常用string函数(收藏)

字符串中字符大小写的变换 1. str.lower() //小写 >>> 'SkatE'.lower() 'skate' 2. str.upper() //大写 >...

Python功能键的读取方法

本文实例讲述了Python功能键的读取方法。分享给大家供大家参考。具体分析如下: 先getch一下得到a,如果等于0或者224,就说明是功能键,再getch下一个得到b,那么这个功能键的...

python pygame实现挡板弹球游戏

python pygame实现挡板弹球游戏

学了一天pygame,用python和pygame写一个简单的挡板弹球游戏 GitHub: EasyBaffleBallGame # -*- coding:utf-8 -*- fr...