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

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. 不同线程不能获取其他线程设置的ThreadLocal里面的值

    背景: 最近在项目用到了ThreadLocal,存放一些值.起线程异步获取ThreadLocal中的值,得到null.这是由于,ThreadLocal.get()会获取当前线程的一个map对象,以Th ...

  2. Jms学习篇二:ActiveMQ

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线 安装 1>下载:到http://activemq.apache.org/download.html 下载最新版本, 解压a ...

  3. Django组件——forms组件

    一.校验字段功能 通过注册用户这个实例来学习校验字段功能. 1.模型:models.py from django.db import models # Create your models here. ...

  4. 18_Condition条件

    [简述] wait()和notify()方法是和synchronized关键字合作使用的. Condition是和重入锁相关联的,通过ReentrantLock.newCondition()生成一个与 ...

  5. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  6. requireJS的优化工具 ---- r.js

    requireJS是javascript的模块加载器,是基于AMD规范实现的. r.js是其提供的对模块进行打包和构建的一个工具 下载 r.js 创建r.js 的配置文件 build.js build ...

  7. SQL还原数据库后孤立用户问题处理(SQL 数据库 拥有对象 无法删除)

    所谓孤立帐户,就是某个数据库的帐户只有用户名而没有登录名,这样的用户在用户库的sysusers系统表中存在,而在master数据库的syslogins中却没有对应的记录 孤立帐户的产生一般是一下两种: ...

  8. java面试之----堆(heap)、栈(stack)和方法区(method)

    JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method)也叫静态存储区. 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的 ...

  9. HCNA配置手工负载分担模式链路聚合

    一.配置手工负载分担模式链路聚合 链路聚合(Link Aggregation)是将—组物理接口捆绑在一起作为一个逻辑接口来增加带宽的一种方法,又称为多接口负载均衡组(Load Sharing Grou ...

  10. mysqlbinlog用法总结

    通过binlog日志统计dml语句,找出操作频繁的表 mysqlbinlog --no-defaults --base64-output=decode-rows -v -v   mysql-bin.0 ...