自己总结一些常用字符串函数,理解比较粗糙

1.字符串内建函数-大小写转换函数

(1)str.capitalize

Help on method_descriptor:

capitalize(...)
     S.capitalize() -> str
    
     Return a capitalized version of S, i.e. make the first character
     have upper case and the rest lower case.

返回值首字母大写,其余小写,不改变数据本身

实例:

a = “start”

a.caplitalize()

Start

(2)str.title()

Help on method_descriptor:

title(...)
     S.title() -> str
    
     Return a titlecased version of S, i.e. words start with title case
     characters, all remaining cased characters have lower case

返回值首字母大写,其余都小写

实例:

a = “start”

a.title()

Start

a = “sTART”

a.title()

Start

(3)str.lower()

Help on method_descriptor:

lower(...)
     S.lower() -> str
    
     Return a copy of the string S converted to lowercase.

返回值字母大写转换为小写,大转小

实例:

a = “START”

a.lower()

start

(4)str.upper()

Help on method_descriptor:

upper(...)
     S.upper() -> str
    
     Return a copy of S converted to uppercase.

返回值字母小写转换为大写,小转大

实例:

a = “start”

a.upper()

START

(5)str.swapcase()

Help on method_descriptor:

swapcase(...)
     S.swapcase() -> str
    
     Return a copy of S with uppercase characters converted to lowercase
     and vice versa.

返回值字母大写转换成小写,小写转换成大写

实例:

a = “Start”

a.swapcase()

sTART

2.字符串内建函数-搜索函数

(1)str.find()

Help on method_descriptor:

find(...)
     S.find(sub[, start[, end]]) -> int
    
     Return the lowest index in S where substring sub is found,
     such that sub is contained within S[start:end].  Optional
     arguments start and end are interpreted as in slice notation.
    
     Return -1 on failure.

S.find(sub[, start[, end]])

搜索字符串在指定的索引范围是否包含子字符串的索引值,否则返回-1

参数:

sub –指定索引的字符串

start -- 开始索引,默认为0。

end -- 结束索引,默认为字符串的长度

实例:

a = “start”

b = “hd”

c = “ar”

a.find(b)

-1

a.find(c)

2

(2)str.index()

Help on method_descriptor:

index(...)
     S.index(sub[, start[, end]]) -> int
    
     Like S.find() but raise ValueError when the substring is not found.

搜索字符串在指定的索引范围是否包含子字符串的索引值,与find方法一样,只不过如果str不在字符串中回报一个异常

实例:

a = “start”

b = “hd”

c = “ar”

a.index(b)

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>

ValueError: substring not found

a.index(c)

2

(3)str.count()

Help on method_descriptor:

count(...)
     S.count(sub[, start[, end]]) -> int
    
     Return the number of non-overlapping occurrences of substring sub in
     string S[start:end].  Optional arguments start and end are
     interpreted as in slice notation.

计算子字符串在指定字符串中出现的次数

实例:

a = “start”

b = “a”

a.count(b)

1

(4)str.endswith()

Help on method_descriptor:

endswith(...)
     S.endswith(suffix[, start[, end]]) -> bool
    
     Return True if S ends with the specified suffix, False otherwise.
     With optional start, test S beginning at that position.
     With optional end, stop comparing S at that position.
     suffix can also be a tuple of strings to try.

如果字符串含有指定的后缀返回True,否则返回False

实例:

a = “start”

b = “a”

a.endswith(b)

False

a = “start”

b = “t”

a.endswith(b)

True

3.字符串内建函数-替换函数

(1)str.replace(old,new)

Help on method_descriptor:

replace(...)
     S.replace(old, new[, count]) -> str
    
     Return a copy of S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.

将old替换为new

实例:

a = “start”

b = “op’

a.replace(‘art’, b)

‘stop’

(2)str.strip(char)

Help on method_descriptor:

strip(...)
     S.strip([chars]) -> str
    
     Return a copy of the string S with leading and trailing
     whitespace removed.
     If chars is given and not None, remove characters in chars instead.

在str的开头和结尾删除char,当char为空时,默认删除空白符

实例:

a = “   start   ”

a.strip()

“start”

(3)str.rstrip()

Help on method_descriptor:

rstrip(...)
     S.rstrip([chars]) -> str
    
     Return a copy of the string S with trailing whitespace removed.
     If chars is given and not None, remove characters in chars instead.

删除str字符串末尾的空格,或换行符号

实例:

a = “ start  ”

a.rstrip()

“  start”

python字符串常用内建函数总结的更多相关文章

  1. Python—字符串常用函数

    Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...

  2. python字符串常用内置方法

    python字符串常用内置方法 定义: 字符串是一个有序的字符的集合,用与存储和表示基本的文本信息. python中引号中间包含的就是字符串. # s1='hello world' # s2=&quo ...

  3. python 字符串常用操作方法

    python 字符串常用操作方法 python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等 1.去除空格 str.strip():删除字符串两边的指定字符,括号的写 ...

  4. python字符串常用操作方法

    python字符串操作常用操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下. 1.去除空格str.strip():删除字符串两边的指定字符,括号的写入指定字符,默 ...

  5. python - 字符串的内建函数

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_3_str_内建函数.py@ide: PyCharm Commu ...

  6. python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式

    # 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...

  7. Python 字符串常用判断函数

    判断字符串常用函数: S代表某字符串 S.isalnum()  所有字符都是数字或字母,为真返回Ture,否则返回False S.isalha()     所有字符都是字母,为真返回Ture,否则返回 ...

  8. python字符串常用的方法解析

    这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...

  9. Python 字符串常用函数

    操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...

随机推荐

  1. CSS实现文本周围插入符号

    CSS实现文本周围插入符号的方案 本文要讨论的是如何在文本的周围插入图标,怎么样控制它们之间的位置关系,通过HTML结构合理性与CSS属性的使用来比较不同方案所实现效果的优缺点. 常见设计稿要求 在文 ...

  2. JVM Guide

    Java Virtual Machine: the Essential Guide October 8th, 2014 - By Alexey Zhebel Introduction Java Vir ...

  3. AE常用代码(标注要素、AE中画带箭头的线、如何获得投影坐标、参考坐标、投影方式、FeatureCount注意事项)

    手上的电脑已经用了将近三年了,想入手一台Surface Pro,所以计划着把电脑上的资料整理下,部分资料打算发到博客上来,资料有同事.也有自己的.也有来自网络的,来源途径太多,也没法详细注明,请见谅! ...

  4. PHP:数字转Excel列头

    转自我的个人博客:阔野飞花 http://www.rexcao.net/archives/169 前段时间升级一个项目的Excel导出功能,这次的列数大概有60多条,在处理过程中发现一个问题,原先做好 ...

  5. sql字段合并与分组聚合

    http://blog.csdn.net/cuixianlong/article/details/74024846 1 字段合并 原始数据如下:表名为Employee ID FirstName Las ...

  6. 到底哪种类型的错误信息会阻止business transaction的保存

    当试图在CRM WebUI保存一个business transaction比如Opportunity时,可能会遇到各种各样的错误消息.有的错误消息会阻止Business transaction被sav ...

  7. 模拟误删除InnoDB ibdata数据文件恢复

    注意:假如误删除 ibdata文件 ,此时千万别把mysqld进程杀死,否则没法挽救. 1.模拟删除ibdata数据文件和重做日志文件: [root@hcdb0 data]# lltotal 4219 ...

  8. 广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

    迭代公式的指数,使用的1+5j,这是个复数.所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形.各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库 http: ...

  9. 牛客练习赛20(ABC)

    A. 礼物 题意: 我从买奥利奥的事情中想出了一个算法题:假设某个店铺有N种不同类型的1元奥利奥和M种不同类型的2元奥利奥,而且余量无限,我的钱有k元,我想把k元都用来买奥利奥,且可以买同类型的奥利奥 ...

  10. CF549BLooksery Party题解

    题目描述 The Looksery company, consisting of nn staff members, is planning another big party. Every empl ...