python(字符串函数)
一.字符串函数
1.首字母大小写
- capitalize()
- title()
name = "xinfangshuo" print (name.capitalize()) print (name.title())
2.upper()方法将字符串中的小写字母转为大写字母
name = "xinfangshuo" #字母全部大写 print (name.upper()) name = "ZHANGsan" print (name.upper())
3.count()统计字符串里某个字符出现的次数
name = "nosnfienvdknvdicn"
print (name.count("n"))
name = "ndsvknoMCLJXCNcwkn"
print (name.count("n",0,8))
4.join()把集合中的字符按自定义的分隔符连接在一起
name = 'Jay' new_name = '.'.join(name) print (new_name) print (new_name[0])
strs = "XFS"
a = list(strs)
a.remove("F")
b = "".join(a)
5.split()把字符串通过指定标识符分割为序列
name = 'J-a-y'
#去除"-"
new_name = name.split('-')
print (new_name)
#无条件连接
result = ''.join(new_name)
print (result)
strs = "XFS"
New_strs = "/".join(strs)
a = New_strs.split("/")
a.remove("F")
b = "".join(a)
print (b)
6.splitlines()按照行('\r', '\r\n', \n')分隔
- 返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
name = "dasl\rdnasl\nsdnaadsaasdas\r\ndaldmas" print (name.splitlines()) print (name.splitlines(True))
7.strip()
- B.strip("ad") --> 删除 B 字符串中开头、结尾处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad' New_strs = strs.strip('ad') print (New_strs) strs = ' aaadnnjlkdamkaad ' New_strs = strs.strip(' ') #删除空格 print (New_strs)
8.lstrip()
- B.lstrip("ad") --> 删除 B 字符串中开头处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad' New_strs = strs.lstrip('ad') print (New_strs)
9.rstrip()
- B.rstrip("ad") --> 删除 B 字符串中结尾处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad' New_strs = strs.rstrip('ad') print (New_strs)
10.startswith()函数判断文本是否以某个字符开始
phone = raw_input("请输入手机号:")
"):
print ("Phone is ok!")
else:
print ("Phone must startswith string '1'")
name = ['张三','李四','王五','赵六','张强','李白','李杜']
count1 = 0
count2 = 0
count3 = 0
for i in name:
if i.startswith('张'):
count1 += 1
elif i.startswith('李'):
count2 += 1
elif i.startswith('王'):
count3 += 1
print ('全班姓张的有%d 人,全班姓李的有%d 人,全班姓王的有%d 人'%(count1,count2,count3))
11.endswith()函数判断文本是否以某个字符结束
#coding=utf-8
text1 = raw_input('请上传您的文档:')
if text1.endswith('.doc'):
print ('上传成功')
else:
print ('上传失败')
12.index()方法检测字符串中是否包含子字符串 str(返回的是该字符串的索引值)
strs = "xinfangshuo"
#找到了,返回字符串的开始索引号
print (strs.index("fang"))
#未找到时报错:ValueError: substring not found
print (strs.index("na"))
13.replace()字符串替换
- replace(old_str,new_old)
#coding=utf-8 strs = "我爱python" print (strs.replace("python","java")) name = "XFS" print (name.replace("F",""))
14.find()从左边开始查询字符串
- (1)find("str",start,end)
- "str":待查的字符
- start:表示开始查询的索引值
- end:表示查询结束的索引值
- (2)当查询到结果后,返回字符串的索引值
strs = "I love python" print (strs.find("love",0,-1)) - (3)当查询不到时,返回的结果为-1
strs = "I love python" print (strs.find("java",0,-1))
15.center(width,fillchar)居中
- 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
- fillchar 默认填充字符为空格。
strs = "python" print (strs.center(100,"-")) #fillchar 默认填充字符为空格 print (strs.center(100))
16.ljust(width,fillchar)方法返回一个原字符串左对齐
- 并使用空格填充至指定长度的新字符串。
- 如果指定的长度小于原字符串的长度则返回原字符串
strs = "python" print (strs.ljust(100,"-")) #fillchar 默认填充字符为空格 print (strs.ljust(100)) #指定的长度小于原字符串的长度则返回原字符串 print (strs.ljust(3,"-"))
17.rjust(width,fillchar) 返回一个原字符串右对齐
- 并使用空格填充至长度 width 的新字符串。
- 如果指定的长度小于字符串的长度则返回原字符串
strs = "python" print (strs.rjust(100,"-")) #fillchar 默认填充字符为空格 print (strs.rjust(100)) #指定的长度小于原字符串的长度则返回原字符串 print (strs.rjust(3,"-"))
18.zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充 0
strs = "python" print (strs.zfill(100))
19.isalnum()方法检测字符串是否由字母/数字组成且不能为空(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isalnum():
print ("Password is right!")
else:
print ("Password is error!")
20.isalpha()方法检测字符串是否只由字母组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isalpha():
print ("Password is right!")
else:
print ("Password is error!")
21.isdigit()方法检测字符串是否只由数字组成(返回的是 True、False)
phone = raw_input("Please input your phone number:")
if phone.isdigit():
print ("Phone number is right!")
else:
print ("Phone number is error!")
22.islower()方法检测字符串是否由小写字母组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.islower():
print ("Password is right!")
else:
print ("Password is error!")
23.isupper() 方法检测字符串中所有的字母是否都为大写(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isupper():
print ("Password is right!")
else:
print ("Password is error!")
24.istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.title():
print ("Password is right!")
else:
print ("Password is error!")
25.isspace() 方法检测字符串是否只由空格组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isspace():
print ("Password is right!")
else:
print ("Password is error!")
python(字符串函数)的更多相关文章
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
- 【276】◀▶ Python 字符串函数说明
参考:Python 字符串函数 01 capitalize 把字符串的第一个字符大写,其他字母变小写. 02 center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...
- (转)python字符串函数
原文:https://www.cnblogs.com/emanlee/p/3616755.html https://blog.csdn.net/luoyhang003/article/details/ ...
- Python 简明教程 --- 8,Python 字符串函数
微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 好代码本身就是最好的文档.当你需要添加一个注释时,你应该考虑如何修改代码才能不需要注释. -- St ...
- python字符串函数总结
字符串函数主要分为: 查找: strs = "this is a line of text for test" index1 = strs.find("is") ...
- 在 R 中使用 Python 字符串函数
sprintf( )函数很强大,但并非适用于所有应用场景.例如,如果一些部分在模板中多次出现,那么就需要多次写一样的参数.这通常会使得代码冗长而且难以修改:sprintf("%s, %d y ...
- python 字符串函数
split函数:将字符串分割成序列 str.split("分隔符") 一般可以这样用 list = [n for n in str.split],这样可以得到一个新的序列 str ...
- python 字符串函数功能快查
0.dir(str)一.有字符发生转换1.capitalize,字符串的第一个字符大写2.casefold,将所有字符小写,Unicode所有字符均适用3.lower,将所有字符小写,只适用ASCii ...
随机推荐
- 搭建Bitcoin全节点
节点搭建 1. 进入 bitcoin 选择 Choose your wallet 2. 选择 Bitcoin Core for Linux 下载 bitcoin-0.17.0.1-x86_64-lin ...
- tf.image.crop_and_resize
https://blog.csdn.net/m0_38024332/article/details/81779544 将图片剪切下来,池化为固定大小.可以快速的对proposal进行池化
- ContextLoadListener & DispatcherServlet 加载顺序以及加载过程
org.springframework.web.context.ContextLoaderListener 1org.springframework.web.servlet.DispatcherSer ...
- 深度强化学习 之 运行环境 mujoco 报错 ERROR: GLEW initalization error: Missing GL version
使用 mujoco环境 运行代码,报错 ERROR: GLEW initalization error: Missing GL version 一直无法解决,发现网址: https://blog. ...
- [LeetCode] 218. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- jvm面试常见题
背景:jvm相关题目面试必问,后面要深入的进行总结. JVM 面试知识整理 jvm调优命令 调优工具 Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Mi ...
- 深度解析qml引擎---(2)绑定(binding)
强烈的希望是人生中比任何欢乐更大的兴奋剂.--尼采 上一篇文章讲了QML引擎加载qml文件的过程,大体过程是,解析qml文件,然后为文件中的每个元素创建对应的c++对象.例如,qml文件中如果使用了T ...
- dockfile构建自己的tomcat
touch Dockerfile 在Dockerfile中输入以下内容 FROM centosMAINTAINER Irish<3395327965@qq.com>#把宿主机当前上下文的 ...
- 百度小程序上传失败 ServerError:30010的原因
最近通过uniapp编译百度智能小程序后上传遇到了报错,错误码为30010. 原因很简单开发者工具和版本库产生了冲突. 两个解决方案,升级开发者工具,降低发布时的版本库 exit;
- Python基础案例1如何实现对应位置上的元素相乘/相加/相减
如何通过a b得到c 本文以“乘”引入,其他运算,类似.案例逻辑如下: a = [[1, 2, 3], [4, 5, 6], [1, 1, 1]]b = [[1, 1, 2], [1, 3, 2], ...