string字符串处理库
字符串作为一种常见的数据类型,在日常中面临各式各样的字符串处理问题,这就要求必须掌握一些常用的字符串处理函数。
字符串的基本需求:查找、修改、删除、判断。
查找
常用于查找到函数有:**find()**、**index()**、**count()**
find()
检测字符串是否包含特定字符,如果包含,则返回开始的索引;否则,返回-1。
str='Welcome to DataScience'print(str.find('Da'))#11print(str.find('wc'))#-1
index()
检测字符串是否包含指定字符,如果包含,则返回开始的索引值;否则,提示错误。
print(str.index('come'))#3print(str.index('Date'))---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-12-85801a960777> in <module>----> 1 print(str.index('Date'))ValueError: substring not found
count()
返回str1在string中指定索引范围内[start, end]出现的次数。
print(str.count('e'))#4print(str.count('Da'))#1
修改
常见的用于字符串修改函数有replace()、split()、join()、capitalize()、title()、upper() 、loewr()、ljust()、rjust()、center()
replace()
把字符串中的 old(旧字符串)替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
- 该函数语法如下:
str.replace(old, new[,max]) - 参数说明:
old— 将被替换的子字符串new— 新字符串,用于替换old子字符串 max— 可选字符串, 替换不超过 max 次str='Welcome to Data Science'print(str.replace("Data", "data"))#Welcome to data Scienceprint(str.replace("e", "E", 3))#WElcomE to Data SciEnce
split()如果 maxsplit有指定值,则仅分割
maxsplit个子字符串;- 语法格式如下:
str.split('分界符', maxSplit) - 参数说明:
maxSplit默认值为-1,表示根据定界符分割所有能分割的 返回值为列表
str='Welcome to Data Science'print(str.split(' ')) #用空格作为分节符#['Welcome', 'to', 'Data', 'Science']print(str.split('e',3))#['W', 'lcom', ' to Data Sci', 'nce']
capitilize()将字符串的首字母大写,其余字母全部小写
- 语法结构如下:
str.capitiliaze() 无参数
str='welcome to Data Science'print(str.capitalize())#Welcome to data science
join()用于将序列中的元素以指定的字符连接生成一个新的字符串。
- 语法结构如下:
str.join(sequence) sequence— 要连接的元素序列。seq='Welcome to Data Science'str='-'print(str.join(seq))#W-e-l-c-o-m-e- -t-o- -D-a-t-a- -S-c-i-e-n-c-e
title()将字符串中的所有单词的首字母大写,其余字母全部小写;
- 值得注意的是,这里单词的区分是以任何标点符号区分的,即,标点符号的前后都是一个独立的单词,字符串最后一个标点除外哦。详细看示例哈
语法结构如下:
str.title()
无参数str='welcome to data science'print(str.title())#Welcome To Data Sciencestr='welcome,to data-science.' #加标点符号print(str.title())#Welcome,To Data-Science.
lower()将字符串的所有字母转换为小写;
语法格式如下:
str.lower()
无参数str='WELCOME TO DATA SCIENCE'print(str.lower())#welcome to data science
upper()将字符串的所有字母转换为大写;
- 语法格式如下:
str.upper()
无参数
str='welcome to data science'print(str.upper())#WELCOME TO DATA SCIENCE
ljust()
- 将字符串左对齐,并使用空格填充至指定长度len;
语法格式如下:
str.ljust(len)str='Welcome to data science'print('str的原长度为%d'%(len(str)))print('str处理后的长度为%d'%(len(str.ljust(30))))print(str.ljust(30))'''str的原长度为23str处理后的长度为30Welcome to data science'''
rjust()将字符串右对齐,并使用空格填充至指定长度
len;语法格式如下:
str.rjust(len)str='Welcome to data science'print('str的原长度为%d'%(len(str)))print('str处理后的长度为%d'%(len(str.rjust(30))))print(str.rjust(30))#str的原长度为23#str处理后的长度为30# Welcome to data science
center()将字符串居中,并使用空格填充至指定长度len;
语法格式如下:
str.center(len)str='Welcome to Data Science'print(str.center(30))print('str的原长度为%d'%(len(str)))print('str处理后的长度为%d'%(len(str.center(30))))# Welcome to Data Science#str的原长度为23#str处理后的长度为30
删除
常用的字符串删除方法有:
**lstrip()**、**rstripe()**、**stripe()**lstrip()去掉字符串左边的空白字符;
语法格式如下:
str.lstrip()str=' Welcome to Data Science'print(str)print(str.lstrip())# Welcome to Data Science#Welcome to Data Science
rstrip()去掉字符串右边的空白字符;
语法格式如下:
str.rstrip()str='Welcome to Data Science 'print(str)print(str.rstrip())#Welcome to Data Science#Welcome to Data Science
strip()去掉字符串两边的空白字符;
语法格式如下:
str.strip()str=' Welcome to Data Science 'print(str)print(str.strip())# Welcome to Data Science#Welcome to Data Science
判断
常见的用于判断字符串的函数有:
startwith()、endwith()、isalpha()、isdigit()、isalnum()、isspace()startswith()检查字符串str是否 以字符串str1开头,若是,则返回True;否则,返回False;
语法结构如下:
str.startswith()str='Welcome to Data Science'print(str.startswith('W'))print(str.startswith('w'))#True#False
endswith()检查字符串str是否 以字符串str1结尾,若是,则返回True;否则,返回False;
语法结构如下:
str.endswith()str='Welcome to Data Science'print(str.endswith('e'))print(str.endswith('c'))#True#False
isalpha()如果字符串str中只包含字母,则返回True;否则,返回False;
语法结构如下:
str.isalpha()str='Welcome to Data Science'print(str.isalpha()) #包含空格#Falsestr_='asd'print(str_.isalpha())#True
isdigit()如果字符串str中只包含数字,则返回True;否则,返回False;
语法结构如下:
str.isdigit()str='Welcome to Data Science'print(str.isdigit())#Falsestr_='12345'print(str_.isdigit())#True
isalnum()检测字符串是否由字母和数字组成。
语法结构如下:
str.isalnum()str='Welcome to Data Science'print(str.isalnum())#Falsestr_='123data'print(str_.isalnum())#True
isspace()如果字符串str中只包含空格,则返回True;否则,返回False;
- 语法结构如下:
str.isspace()str='Welcome to Data Science'print(str.isspace())#False
