文章目录

  • str类的其他方法 (Cont.)
    • str.encode(encoding="utf-8", errors="strict")
    • str.startswith(prefix[, start[, end]])与str.endswith(suffix [,start [,end]])
    • str.strip([chars])
    • str.swapcase()
    • str.title()

str类的其他方法 (Cont.)

str.encode(encoding=“utf-8”, errors=“strict”)

TODO:

str.startswith(prefix[, start[, end]])与str.endswith(suffix [,start [,end]])

startswith(),如果字符串以指定的prefix起始,则返回True,否则返回False。要查找的prefix可以是一个前缀的元组。可选的start,指定要测试的起始字符位置(含),end指定结束字符位置(不含)。

endswith(),如果字符串以指定的suffix结尾,则返回True,否则返回False。要查找的suffix可以是一个后缀的元组。可选的start,指定要测试的起始字符位置(含),end指定结束字符位置(不含)。

>>> "我是中国人".startswith('我') # surely return True
True# actually test "是中国人"
>>> "我是中国人".startswith('我', 1)
False# return True if hit one of prefix in the tuple
>>> "我是中国人".startswith(('我', '你'))
True# return False if hit no prefix in the tuple
>>> "我是中国人".startswith(('他', '你'))
False>>> "Hello".endswith("o") # Hello ends with 'o'
True
>>> "Hello".endswith("llo") # Hello ends with 'llo'
True# Hello does not end with "LLO" or "l"; case-sensitive search
>>> "Hello".endswith("LLO")
False
>>> "Hello".endswith("l")
False# actually test "He", which does not end with 'l'
>>> "Hello".endswith("l",0,2)
False# actually test "Hel", which ends with 'l'
>>> "Hello".endswith("l",0,3)
True# actually test "Hell", which ends with 'l'
>>> "Hello".endswith("l",0,4)
True# actually test "Hello", which does not end with 'l'
>>> "Hello".endswith("l",0,5)
False# return True if hit any suffix in the tuple
>>> "Hello".endswith(("o", "lo"))
True
>>> "Hello".endswith(("o", "lo", "llo"))
True
>>> "Hello".endswith(("o", "lo", "llo", "LLO"))
True# return False if hit no suffix in the tuple
>>> "Hello".endswith(("O", "LO", "LL", "LLO"))
False

str.strip([chars])

返回一个新字符串,chars指定了字符集合,源字符串首尾任何位于chars集合中的字符都会被删除掉。如果未指定chars或者chars为None,则删除首尾的空白字符(包括制表符,空格,换行符等)。

>>> '   spacious       \n  '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'
>>> '#....... Section 3.2.1 Issue #32 .......'.strip('.#! ')
'Section 3.2.1 Issue #32'

str.swapcase()

返回一个新字符串,将源字符串中的大小写反转,即小写变大写,大写变小写。但是要注意两次反转未必一定得到源字符串,即s.swapcase().swapcase() == s不一定肯定为True。

>>> "Hello".swapcase()
'hELLO'>>> "Hello".swapcase().swapcase()
'Hello'# if is not alway the case that s.swapcase().swapcase() == s
>>> 'ß'.swapcase()
'SS'
>>> 'ß'.swapcase().swapcase()
'ss'

str.title()

返回一个新字符串,源字符串中的每个“单词”首字母大写,其余字母小写。

# an acceptable title
>>> "a story of great dective homoles".title()
'A Story Of Great Dective Homoles'

但是由于Python使用了一种语言独立的单词切分算法,有时候结果不尽人意。参看下例:

# not a very satisfying title,
# --- Do NOT want to see 'Re and 'S in the title
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"

原因是单撇号’也作为单词分隔符,将缩写与所有格形式后面的字母作为独立的单词了。可以使用正则表达式来规避这个小问题。

import re
def titlecase(s):return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(), s)print(titlecase("they're bill's friends."))--- Console Output ---
They're Bill's Friends.

【Python】【Python库】Python3.7.2 - 字符串str类 (2)相关推荐

  1. 【Python】【Python库】Python3.7.2 - 字符串str类 (1)

    文章目录 str类简介 str的构造函数/方法 str的其他方法 str.capitalize() str.casefold() 与str.lower() str.center(width [, fi ...

  2. Python3 数字转换为字符串str()函数

  3. 转Python 标准库 urllib2 的使用细节

    Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库.这里总结了一些 urllib2 库的使用细节. 1 P ...

  4. Python 标准库 urllib2 的使用细节

    刚好用到,这篇文章写得不错,转过来收藏.    转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...

  5. python格式化字符串语法_详解Python3 中的字符串格式化语法

    一.旧式的字符串格式化 % 操作符 参考以下示例: >>> name = "Eric" >>> "Hello, %s." % ...

  6. python 内置方法赋值_Python内置数据结构之字符串str

    1. 数据结构回顾 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于字符串,但是字符串是不可变序列,因此所有的元素赋值和切片赋值都是非法的. >>> ...

  7. python字符串str_python3 字符串str 教程

    var1 = 'Hello World!' var2 = "Python Programming" Python 访问子字符串,可以使用方括号来索引或截取(切片)获取子字符串,如下 ...

  8. 4.Python数据容器之字符串(str)

    字符串及相关操作 字符串是Python中最常用的数据类型,其重要性不必多言 在Python3中,所有的字符串都是Unicode字符串 1.字符串的定义 (1) s1 = ""(2) ...

  9. Python base64库 解码本地txt文本字符串

    Python base64库 解码本地txt文本字符串 使用base64还原由图片加密而成的字符串. Raw字符串: iVBORw0KGgoAAAANSUhEUgAAAtoAAALaCAYAAAAP7 ...

最新文章

  1. Go 学习笔记(1)— Ubuntu 系统 Go 环境搭建、VS Code 配置 Go 开发环境、VS Code 远程开发配置
  2. JS触发Click操作以及获得事件源(转)
  3. 如何禁止用户连续点击一个按钮事件详细JS
  4. JavaScript核心参考手册.chm
  5. string find简析
  6. TComboBox下拉取值
  7. iphone以旧换新活动_【武商襄阳购物中心】 参与iphone以旧换新活动,最高可抵4300元,旧机可享10%额外补贴...
  8. java对象类型有哪些_Java中常用的对象数据类型有哪些?它们分别又占多少个字节呢?...
  9. TortoiseGit 客户端安装及使用
  10. C#实现 Linq 序列的Distinct—— IEnumerable.Distinct()——IEqualityComparer
  11. python time库_python中time库的实例使用方法
  12. python—tf.keras.backend.clear_session()
  13. 这37个自学网站,一年让你省下十几万。钱买辆车他不香嘛
  14. Server-sent events(SSE) EventSource 客户端使用与服务器基础实现(基于Node.js)
  15. 学英语查单词:快乐英语,简单生活,why not Bing EngKoo!?
  16. Linux条件变量(pthread_cond)示例
  17. c语言实现万能求积分
  18. 还在用百度查找资源?不要落后啦,5款出奇好用的资源网送给你!
  19. 2021年CVPR论文Deep Two-View Structure-from-Motion Revisited阅读笔记
  20. Translate Aticle

热门文章

  1. java Stream 流
  2. C++中cin的常用用法
  3. mysql本周数据没有填充_MySql查询本周、本月、本年数据(没有数据则补全0)
  4. spring boot整合shiro继承redis_Springboot+Shiro+redis整合
  5. 从WordCount看Spark大数据处理的核心机制(1)
  6. Hibernate执行Update操作之后查询跟新的语句出错
  7. day15 java的抽象类
  8. mongodb更新操作符
  9. 网络实用技术基础模拟测试2_网络安全工程师教你:Kali Linux之Metasploit渗透测试基础(五)...
  10. win7怎么设置开机密码_win7系统设置电脑密码的方法