s = "hello world"
print(s.capitalize())
print(s.title())
# 结果如下
Hello world
Hello World
name = "xinfangshuo"
#字母全部大写
print (name.upper())
name = "ZHANGsan"
print (name.upper())
s = "abcdEFG"
print(s.swapcase())
---> ABCDefg
name = "nosnfienvdknvdicn"
print (name.count("n"))
name = "ndsvknoMCLJXCNcwkn"
print (name.count("n",0,8))
first_name = "zhang"
second_name = "san"
name = "".join([first_name,second_name])
print(name)
---> zhangsan
name = 'Jay'
new_name = '.'.join(name)
print (new_name)
print (new_name[0])
a = "I"
b = "am"
c = "Chinese"
d = " ".join([a,b,c])
print(d)
---> I am Chinese
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)
- 返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
name = "dasl\rdnasl\nsdnaadsaasdas\r\ndaldmas"
print (name.splitlines())
print (name.splitlines(True))
8、strip()方法用于移除字符串头尾指定的字符(默认为空格)或字符序列,例如:/n, /r, /t, ' '。
- 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
name = " zhangsan "
print(name)
print(name.strip())
---> zhangsan
---> zhangsan
- B.strip("ad") --> 删除 B 字符串中开头、结尾处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad'
New_strs = strs.strip('ad')
print (New_strs)
strs = ' aaadnnjlkdamkaad '
New_strs = strs.strip(' ') #删除空格
print (New_strs)
9、lstrip()
- B.lstrip("ad") --> 删除 B 字符串中开头处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad'
New_strs = strs.lstrip('ad')
print (New_strs)
10、rstrip()
- B.rstrip("ad") --> 删除 B 字符串中结尾处,位于 ad 删除序列的字符
strs = 'aaadnnjlkdamkaad'
New_strs = strs.rstrip('ad')
print (New_strs)
11、startswith()函数判断文本是否以某个字符开始
phone = raw_input("请输入手机号:")
if phone.startswith("1"):
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))
12、endswith()函数判断文本是否以某个字符结束
#coding=utf-8
text1 = raw_input('请上传您的文档:')
if text1.endswith('.doc'):
print ('上传成功')
else:
print ('上传失败')
13、index()方法检测字符串中是否包含子字符串 str(返回的是该字符串的索引值)
strs = "xinfangshuo"
#找到了,返回字符串的开始索引号
print (strs.index("fang"))
#未找到时报错:ValueError: substring not found
print (strs.index("na"))
14、replace()字符串替换
#coding=utf-8
strs = "我爱python"
print (strs.replace("python","java"))
name = "XFS"
print (name.replace("F",""))
15、find()从左边开始查询字符串
- (1)find("str",start,end)
- "str":待查的字符
- start:表示开始查询的索引值
- end:表示查询结束的索引值
- (2)当查询到结果后,返回字符串的索引值
strs = "I love python"
print (strs.find("love",0,-1))
strs = "I love python"
print (strs.find("java",0,-1))
16、center(width,fillchar)居中
- 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
- fillchar 默认填充字符为空格。
strs = "python"
print (strs.center(100,"-"))
#fillchar 默认填充字符为空格
print (strs.center(100))
17、ljust(width,fillchar)方法返回一个原字符串左对齐
- 并使用空格填充至指定长度的新字符串。
- 如果指定的长度小于原字符串的长度则返回原字符串
strs = "python"
print (strs.ljust(100,"-"))
#fillchar 默认填充字符为空格
print (strs.ljust(100))
#指定的长度小于原字符串的长度则返回原字符串
print (strs.ljust(3,"-"))
18、rjust(width,fillchar) 返回一个原字符串右对齐
- 并使用空格填充至长度 width 的新字符串。
- 如果指定的长度小于字符串的长度则返回原字符串
strs = "python"
print (strs.rjust(100,"-"))
#fillchar 默认填充字符为空格
print (strs.rjust(100))
#指定的长度小于原字符串的长度则返回原字符串
print (strs.rjust(3,"-"))
19、zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充 0
strs = "python"
print (strs.zfill(100))
20、isalnum()方法检测字符串是否由字母/数字组成且不能为空(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isalnum():
print ("Password is right!")
else:
print ("Password is error!")
21、isalpha()方法检测字符串是否只由字母组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isalpha():
print ("Password is right!")
else:
print ("Password is error!")
22、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!")
23、islower()方法检测字符串是否由小写字母组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.islower():
print ("Password is right!")
else:
print ("Password is error!")
24、isupper() 方法检测字符串中所有的字母是否都为大写(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isupper():
print ("Password is right!")
else:
print ("Password is error!")
25、istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.title():
print ("Password is right!")
else:
print ("Password is error!")
26、isspace() 方法检测字符串是否只由空格组成(返回的是 True、False)
pwd = raw_input("Please input your password:")
if pwd.isspace():
print ("Password is right!")
else:
print ("Password is error!")