Python string常用函数
2017-07-03 23:26:08
1、.replace(self, old, new, count=-1)
replace()函数将旧字符串替换为新字符串,最后一个参数count为可选项,表示替换最多count次(小于count)。
注意这种替换返回替换后的字符串,源字符串是不改变的。
s='ABCDEF'
out=s.replace('EF','ef')
print(s)
print(out) 输出:
ABCDEF
ABCDef
2、.find(self, sub, start=0, end=len)
find()函数返回第一次字符数sub的下标索引,默认情况下是扫描整个字符串,不过可以自行设定start,end,和stl一致的是,这里的起始区间前闭后开。
没有找到的,返回-1
s='ABCDEF'
print(s.find('BC',0,1))
print(s.find('CD')) 输出:
-1
2
.rfind()函数则是从右向左进行搜索,即返回索引值最大的sub字串的下标,若没有搜索到,则返回-1。
3、.split(str=' ',num)
split()函数可以将字符串通过str进行分割,缺省为空格,分割次数为num,返回一个列表。
s = 'microsoft@qq.com'
a = s.split('@')
print(a) #['microsoft', 'qq.com']
4、.strip(char c=‘ ’)
strip()函数用于删除字符串头尾的指定字符c,缺省条件下为空格。lstrip()和rstrip()分别用于删除左右的指定字符。如同replace(),str本身是无法直接被修改的。
s = 'microsoft@qq.com!!!!!!!!!'
s=s.strip('!')
print(s) #microsoft@qq.com
5、.count(str, beg=0, end=len(string))
s = 'microsoft@qq.com!!!!!!!!!'
x = s.count('!')
print(x)
Python string常用函数的更多相关文章
- C++ string 常用函数
C++ String常用函数 一,类型别名 size_type 无符号整型 iterator 迭代器类型 const_iterator 只读迭代器 reverse_iterator 逆序迭代器 con ...
- C#string常用函数总结
补充: 1:在C语言里 char占1个字节 而在C#,Java里char占两个字节 数据库里char 中汉占两个字节 字母数字占一个字 2:string ...
- Python 字符串常用函数
操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- Python—字符串常用函数
Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...
- 【STL】string 常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- 【转】string常用函数
原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...
- String 类的实现(5)String常用函数
2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...
- stl string常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
随机推荐
- bootstrap modal 垂直居中对齐
bootstrap modal 垂直居中对齐 文章参考 http://www.bubuko.com/infodetail-666582.html http://v3.bootcss.com/Jav ...
- redis error It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING
应用redis出现如下错误 It was not possible to connect to the redis server(s); to create a disconnected multip ...
- MAX_STATEMENT_TIME uses confusing syntax
From https://bugs.mysql.com/bug.php?id=72540 [5 May 2014 18:46] Morgan Tocker Description: Via C ...
- marquee实现跑马灯
<!DOCTYPE html><html> <head><title>跑马灯大全</title> </head> <bod ...
- C++飞机大战
#include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...
- Python 成对处理数据 zip()
当你想成对处理数据的时候zip() 函数是很有用的.比如,假设你头列表和一个值列表,就像下面这样: headers = ['name', 'shares', 'price'] values = ['A ...
- 下载及安装selenium IDE
安装方法1:可以从官方网站下载:http://seleniumhq.org/download/,但是由于selenium服务器在美国,中美的网络经常不稳定,经常会连接失败,导致下载不了 可以找一下se ...
- 谈话准备.xmind 思维导图模版
谈话准备.xmind 思维导图模版,谈话准备.xmind.zip 谈话准备 目标 1…… 2…… 3…… 影响决定因素 进程 经费 策略 问题 客户 竞争对手 竞争对手 潜在 现存 问题 个性 团队 ...
- Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath
1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...
- iOS xcode创建静态库封装自己的SDK及使用
https://www.cnblogs.com/JustForHappy/p/5773039.html 一,静态库和动态库的区别在这里就不说了,个人感觉如果是自己封装提供别人下载的话应该是静态库比较方 ...