Python 字符串方法 | 第一讲(find,rfind,startswith,endswith,islower,isupper,lower,upper,swapcase & title)

本贴最后更新于 2267 天前,其中的信息可能已经物是人非

原网址:https://www.geeksforgeeks.org/python-string-methods-set-1-find-rfind-startwith-endwith-islower-isupper-lower-upper-swapcase-title

Python 字符串方法 | 第一讲(find,rfind,startswith,endswith,islower,isupper,lower,upper,swapcase & title)

一些字符串的基础已经在下面这些文章里涵盖了:

字符串-第一部分
字符串-第二部分

重要的字符串方法将在这篇文章里讲解。

1. find(“string”, beg, end) 这个函数被用来在字符串中找到子字符串的开始位置。它有三个参数, 子字符串 , 起始索引( 默认为0) and 结束索引(默认为字符串长度).

  • 如果在字符串中没有找到给定的子字符串,那么会返回-1。
  • 它会返回子字符串的开始位置索引

2. rfind(“string”, beg, end) :- 这个函数和 find()有几乎相同的功能, 但是它返回子字符串的结束位置索引

# Python code to demonstrate working of # find() and rfind() str = "geeksforgeeks is for geeks" str2 = "geeks" # using find() to find first occurrence of str2 # returns 8 print ("The first occurrence of str2 is at : ", end="") print (str.find( str2, 4) ) # using rfind() to find last occurrence of str2 # returns 21 print ("The last occurrence of str2 is at : ", end="") print ( str.rfind( str2, 4) )

Output:

The first occurrence of str2 is at : 8 The last occurrence of str2 is at : 21

3. startswith(“string”, beg, end) :- 如果字符串是以指定的子字符串开头的,那么返回 True,否则返回 False。

4. endswith(“string”, beg, end) :- 如果字符串是以指定的子字符串结尾的,那么返回 True,否则返回 False。

# Python code to demonstrate working of # startswith() and endswith() str = "geeks" str1 = "geeksforgeeksportal" # using startswith() to find if str # starts with str1 if str1.startswith(str): print ("str1 begins with : " + str) else : print ("str1 does not begin with : "+ str) # using endswith() to find # if str ends with str1 if str1.endswith(str): print ("str1 ends with : " + str) else : print ("str1 does not end with : " + str) )

Output:

str1 begins with : geeks str1 does not end with : geeks

5. islower(“string”) :- 如果字符串中所有字符都是 小写的 ,那么返回 True,否则返回 False。

6. isupper(“string”) :- 如果字符串中所有字符都是因为大写的,那么返回 True,否则返回 False。

# Python code to demonstrate working of # isupper() and islower() str = "GeeksforGeeks" str1 = "geeks" # checking if all characters in str are upper cased if str.isupper() : print ("All characters in str are upper cased") else : print ("All characters in str are not upper cased") # checking if all characters in str1 are lower cased if str1.islower() : print ("All characters in str1 are lower cased") else : print ("All characters in str1 are not lower cased")

Output:

All characters in str are not upper cased All characters in str1 are lower cased

7. lower() :- 这个函数返回一个所有字符都 被转化成小写字母的新字符串。

8. upper():- 这个函数返回一个所有字符都被转化成大写字母的新字符串。

9. swapcase() :- 这个函数被用来转换字符串中每一个字符的大小写。

10. title() :- 这个函数把字符串中的每个单词首字母转换为大写,其余则为小写。

# Python code to demonstrate working of # upper(), lower(), swapcase() and title() str = "GeeksForGeeks is fOr GeeKs" # Coverting string into its lower case str1 = str.lower(); print (" The lower case converted string is : " + str1) # Coverting string into its upper case str2 = str.upper(); print (" The upper case converted string is : " + str2) # Coverting string into its swapped case str3 = str.swapcase(); print (" The swap case converted string is : " + str3) # Coverting string into its title case str4 = str.title(); print (" The title case converted string is : " + str4)

Output:

The lower case converted string is : geeksforgeeks is for geeks The upper case converted string is : GEEKSFORGEEKS IS FOR GEEKS The swap case converted string is : gEEKSfORgEEKS IS FoR gEEkS The title case converted string is : Geeksforgeeks Is For Geeks
  • Python

    Python 是一种面向对象、直译式电脑编程语言,具有近二十年的发展历史,成熟且稳定。它包含了一组完善而且容易理解的标准库,能够轻松完成很多常见的任务。它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用缩进来定义语句块。

    554 引用 • 675 回帖
  • 字符串
    30 引用 • 57 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 面试

    面试造航母,上班拧螺丝。多面试,少加班。

    326 引用 • 1395 回帖 • 2 关注
  • C

    C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

    86 引用 • 165 回帖
  • 印象笔记
    3 引用 • 16 回帖 • 1 关注
  • 书籍

    宋真宗赵恒曾经说过:“书中自有黄金屋,书中自有颜如玉。”

    84 引用 • 414 回帖
  • 人工智能

    人工智能(Artificial Intelligence)是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门技术科学。

    115 引用 • 319 回帖
  • Kotlin

    Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,由 JetBrains 设计开发并开源。Kotlin 可以编译成 Java 字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。在 Google I/O 2017 中,Google 宣布 Kotlin 成为 Android 官方开发语言。

    19 引用 • 33 回帖 • 87 关注
  • 域名

    域名(Domain Name),简称域名、网域,是由一串用点分隔的名字组成的 Internet 上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的电子方位(有时也指地理位置)。

    43 引用 • 208 回帖 • 1 关注
  • MyBatis

    MyBatis 本是 Apache 软件基金会 的一个开源项目 iBatis,2010 年这个项目由 Apache 软件基金会迁移到了 google code,并且改名为 MyBatis ,2013 年 11 月再次迁移到了 GitHub。

    173 引用 • 414 回帖 • 363 关注
  • V2EX

    V2EX 是创意工作者们的社区。这里目前汇聚了超过 400,000 名主要来自互联网行业、游戏行业和媒体行业的创意工作者。V2EX 希望能够成为创意工作者们的生活和事业的一部分。

    16 引用 • 236 回帖 • 240 关注
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 370 关注
  • Dubbo

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是 [阿里巴巴] SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

    60 引用 • 82 回帖 • 618 关注
  • ReactiveX

    ReactiveX 是一个专注于异步编程与控制可观察数据(或者事件)流的 API。它组合了观察者模式,迭代器模式和函数式编程的优秀思想。

    1 引用 • 2 回帖 • 179 关注
  • Pipe

    Pipe 是一款小而美的开源博客平台。Pipe 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    134 引用 • 1127 回帖 • 110 关注
  • OneNote
    1 引用 • 3 回帖
  • OpenStack

    OpenStack 是一个云操作系统,通过数据中心可控制大型的计算、存储、网络等资源池。所有的管理通过前端界面管理员就可以完成,同样也可以通过 Web 接口让最终用户部署资源。

    10 引用 • 1 关注
  • Ruby

    Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发。在 Ruby 社区,松本也被称为马茨(Matz)。

    7 引用 • 31 回帖 • 270 关注
  • Tomcat

    Tomcat 最早是由 Sun Microsystems 开发的一个 Servlet 容器,在 1999 年被捐献给 ASF(Apache Software Foundation),隶属于 Jakarta 项目,现在已经独立为一个顶级项目。Tomcat 主要实现了 JavaEE 中的 Servlet、JSP 规范,同时也提供 HTTP 服务,是市场上非常流行的 Java Web 容器。

    162 引用 • 529 回帖 • 9 关注
  • Shell

    Shell 脚本与 Windows/Dos 下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比 Windows 下的批处理更强大,比用其他编程程序编辑的程序效率更高,因为它使用了 Linux/Unix 下的命令。

    125 引用 • 74 回帖 • 1 关注
  • React

    React 是 Facebook 开源的一个用于构建 UI 的 JavaScript 库。

    192 引用 • 291 回帖 • 367 关注
  • Electron

    Electron 基于 Chromium 和 Node.js,让你可以使用 HTML、CSS 和 JavaScript 构建应用。它是一个由 GitHub 及众多贡献者组成的活跃社区共同维护的开源项目,兼容 Mac、Windows 和 Linux,它构建的应用可在这三个操作系统上面运行。

    15 引用 • 136 回帖 • 3 关注
  • AWS
    11 引用 • 28 回帖 • 6 关注
  • uTools

    uTools 是一个极简、插件化、跨平台的现代桌面软件。通过自由选配丰富的插件,打造你得心应手的工具集合。

    7 引用 • 28 回帖 • 1 关注
  • 开源

    Open Source, Open Mind, Open Sight, Open Future!

    415 引用 • 3598 回帖
  • HBase

    HBase 是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的 Google 论文 “Bigtable:一个结构化数据的分布式存储系统”。就像 Bigtable 利用了 Google 文件系统所提供的分布式数据存储一样,HBase 在 Hadoop 之上提供了类似于 Bigtable 的能力。

    17 引用 • 6 回帖 • 71 关注
  • Sym

    Sym 是一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)系统平台。

    下一代的社区系统,为未来而构建

    524 引用 • 4601 回帖 • 710 关注
  • RIP

    愿逝者安息!

    8 引用 • 92 回帖 • 408 关注
  • Hexo

    Hexo 是一款快速、简洁且高效的博客框架,使用 Node.js 编写。

    22 引用 • 148 回帖 • 11 关注