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

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. Js浮动广告效果实现

    第一种 漂浮广告 不符合W3CJavaScript漂浮广告代码,很不错,代码精简,不过一次只有漂一个,复制就能用了.希望站长朋友喜欢. <html> <head> <ti ...

  2. .NET开源工作流RoadFlow-表单设计-附件管理

    在表单中添加一个附件管理的控件: 文件类型:指定可以上传的文件类型.

  3. matlab练习程序(生成黑白网格)

    提供了两种生成方法,一个是自己编程实现,比较灵活:另一个是调用系统的checkerboard函数,似乎只能生成8*8网格. 至于用途,也许可以用来下国际象棋. 自己函数生成: 系统函数生成: 代码如下 ...

  4. memcached与spring集成

    一.背景 销售CRM(项目A)将负责管理项目信息系统(项目B)的支付与权限 上级要求为避免频繁调用CRM接口,中间放一级缓存,但要做到缓存中保证最新数据 因项目B已使用memcache作缓存,所以决定 ...

  5. C#使用Process类杀死进程,执行命令等

    c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...

  6. 狂欢圣诞节,Azure云邀你一起云端跑酷!

    想要速度更快? 希望绕过障碍? 还想安全抵达目的地? …… 平安夜夜游时的各种畅想,不也正是 IT 同事所追求的,更是业务发展的终极目标呀. 好啦,小编说的太多了,这样不好不好.过节休息不谈工作,那就 ...

  7. sqlserver内存管理之lazy writer

    任何在缓冲区被修改的页都会被标记为“脏”页.将这个脏页写入到数据磁盘就是CheckPoint或者Lazy Writer的工作.前面就大体说了一下checkpoint,这会就简单的理解一下这个lazy ...

  8. HTTP Strict Transport Security

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. 作用 一个网站接受一个HTTP的 ...

  9. Android端访问服务器核心代码

  10. rac数据库默认sql tuning advisor,导致大量library cache lock

    rac数据库默认sql tuning advisor,导致大量library cache lock 问题现象:客户反映周六周日固定十点钟,一个程序会特别慢(大概10分钟),平时1到2秒.查看当时的日志 ...