在Python中操作字符串之startswith()方法的使用

yipeiwu_com7年前Python基础

 startswith()方法检查字符串是否以str开始,任选限制匹配与给定索引的开始和结束。
语法

以下是startswith()方法的语法:

str.startswith(str, beg=0,end=len(string));

参数

  •     str -- 这是要检查的字符串。
  •     beg -- 这是可选的参数设置匹配边界的初始索引。
  •     end -- 这是可选的参数设置匹配边界的结束索引。

返回值

如果找到匹配的字符串此方法返回true,否则为false。
例子

下面的例子显示了startswith()方法的使用。

#!/usr/bin/python

str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', 2, 4 );
print str.startswith( 'this', 2, 4 );

当我们运行上面的程序,它会产生以下结果:

True
True
False

相关文章

python远程调用rpc模块xmlrpclib的方法

RPC(Remote Procedure Call Protocol)是远程调用协议,它通过网络请求服务到远端服务器,服务器根据请求做出响应,将结果返回 它是一种C/S模式,客户端可以调...

python自动截取需要区域,进行图像识别的方法

实例如下所示: import os os.chdir("G:\Python1\Lib\site-packages\pytesser") from pytesser import *...

Python简直是万能的,这5大主要用途你一定要知道!(推荐)

从2015开始国内就开始慢慢接触Python了,从16年开始Python就已经在国内的热度更高了,目前也可以算的上"全民Python"了。 众所周知小学生的教材里面已经有Python了,...

对Python中9种生成新对象的方法总结

先定义一个类: class Point: def __init__(self, x, y): self.x = x self.y = y 下面我们使用9种方法来生...

Python Django Cookie 简单用法解析

Python Django Cookie 简单用法解析

home.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT...