一,摘自官方API  https://docs.python.org/3/library/stdtypes.html#methods

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return Falseprefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

二,摘自 https://www.runoob.com/python/att-string-startswith.html

描述:

Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

startswith()方法语法:

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

参数:

  • str -- 检测的字符串。
  • strbeg -- 可选参数用于设置字符串检测的起始位置。
  • strend -- 可选参数用于设置字符串检测的结束位置。

返回值:

如果检测到字符串则返回True,否则返回False。

实例:

#!/usr/bin/python

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

结果:

True
True
False

拓展:

有多个指定开头的字符串需要判断时,可以给prefix参数传一个元组

示例:

str1 = 'a-123'
str2 = 'b-123'
str3 = 'c-123'
str4 = 'd-123' pre_list = ['a', 'b']
pre_tuple = ('a', 'b')
print(str1.startswith(tuple(pre_list)))
print(str2.startswith(pre_tuple))
print(str3.startswith(pre_tuple))
print(str4.startswith(tuple(pre_list)))

结果:

True
True
False
False

备注:tuple类型和list类型可以互转~~

元组与列表的区别在于:元组比列表的运算速度快,而且元组的数据比较安全。元组是不可改变的,为了保护其内容不被外部接口修改,不具有 append,extend,remove,pop,index这些功能;而列表是可更改的。所有有些时候我们需要两者相互转换,tuple()相当于冻结一个列表,而list()相当于解冻一个元组。可以根据需要定义

Python String startswith() Method的更多相关文章

  1. [Python] String strip() Method

    Description The method strip() returns a copy of the string in which all chars have been stripped fr ...

  2. python string

    string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...

  3. Python string objects implementation

    http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...

  4. python string module

    String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...

  5. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  6. 在Javascript中使用String.startsWith和endsWith

    在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...

  7. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  8. String.Join Method

    Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ...

  9. Python string interning原理

    原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...

随机推荐

  1. vSphere 计算vMotion的迁移原理

    1. 计算vMotion 的应用场景 1). 计划内停机维护 2). 提高资源的利用率 2. 计算vMotion 需求: 1).共享存储 vMotion需要解决的核心问题就是:将VMs的内存从源ESX ...

  2. 使用命令行连接远程DB2数据库

    1. 打开命令窗口 win + r  输入 db2cmd 2. 首先在客户机上对远程节点进行编目 CATALOG  TCPIP                    //编目一个TCP/IP节点NOD ...

  3. 我的C语言的新开端<graphics.h>

    进一步接触C语言<graphics.h> #include<stdio.h> #include<graphics.h> #include<conio.h> ...

  4. Java连载68-数组的拷贝、二维数组

    一.数组的拷贝 函数arraycopy(),参数为:源数组.源数组的开始下标.目标数组.目标数组的开始下标.拷贝长度 package com.bjpowernode.java_learning; ​ ...

  5. 记一次 springboot 参数解析 bug调试 HandlerMethodArgumentResolver

    情况描述 前端输入框输入中文的横线 -- ,到后台接收时变成了 &madsh;$mdash 正常应该显示成这样: bug调试思路记录 最开始完全没有向调试源码方面想,试了不少方法,都没解决,没 ...

  6. python三大神器===》生成器

    1. 认识生成器 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成下一 ...

  7. MongoDB_04_插入和查询

    案列需求: 存在文章评论的数据存放到MongoDB中,数据结构参考如下: 数据库:articledb 专栏文章评论 comment / / 字段名称 字段含义 字段类型 备注 _id ID Objec ...

  8. Codeforces735D Taxes(哥德巴赫猜想)

    题意:已知n元需缴税为n的最大因子x元.现通过将n元分成k份的方式来减少缴税.问通过这种处理方式需缴纳的税费. 分析: 1.若n为素数,不需分解,可得1 2.若n为偶数,由哥德巴赫猜想:一个大于2的偶 ...

  9. 如何禁用AD OU 下面的邮箱用户的Exchange ActiveSync 和 适用于设备的OWA

    Get-Mailbox -OrganizationalUnit QQ禁用名单 | Set-CASMailbox  -ActiveSyncEnabled $false -OWAforDevicesEna ...

  10. 官网英文版学习——RabbitMQ学习笔记(九)总结

    RabbitMQ与spingboot相整合,主要步骤也很简单: 一.首先需要的是添加依赖 二.需要设置属性,属性主要是设置rabbitmq的地址端口,用户名密码回调等需要用到的一些常量,在整合过程中, ...