声明一个字符串,默认可以调用内置函数
大小写相关的内置函数:
capitalize() , title() , upper() , lower()
message = 'tracy is a beautiful girl'
msg= message.capitalize()
print(msg)
msg= message.title()
print(msg)
result= msg.istitle()
print(result)
msg= message.upper()
print(msg)
msg= message.lower()
print(msg)
output:
Tracy is a beautiful girl
Tracy Is A Beautiful Girl
True
TRACY IS A BEAUTIFUL GIRL
tracy is a beautiful girl
应用举例:生成四位随机验证码()
s = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
code = ''
import random
for i in range(4):
ran = random.randint(0,len(s)-1)
code += s[ran]
print('验证码:'+code)
user_input = input('请输入验证码:')
if user_input.lower() == code.lower():
print('验证码输入正确!')
else:
print('验证码错误!')
output
验证码:SuoC
请输入验证码:suoc
验证码输入正确!
查找及替换相关内置函数:
find() , rfind() , lfind() , index() , lindex() , replace()
s1 = 'index tracy is lucky girl'
result = 'R' in s1
print(result)
#'R'不在字符串s1中,返回结果False
False
s1 = 'index tracy is lucky girl'
position = s1.find('R')
print(position)
# 返回值为-1,则代表没有找到
-1
s1 = 'index tracy is lucky girl'
position = s1.find('l')
print(position)
# 如果可以找到,则返回字母第一次出现的位置
15
s1 = 'index tracy is lucky girl'
position = s1.find('r',position+1,len(s1)-5)
print(position)
# 指定开始位置查找
7
ur1 = 'http://www.baidu.com/img/bd_logo1.png'
p = ur1.rfind('/')
print(p)
filename = ur1[p+1:]
print(filename)
p1 = ur1.rfind('.')
kz = ur1[p1+1:]
print(kz)
# rfind() 从右侧开始查找
24
bd_logo1.png
png
‘’‘
index(str,beg=0,end=len(string))
跟find方法一样,str不在字符串中会报一个异常
’‘’
p = 'hello'.index(X)
NameError Traceback (most recent call last)
in
----> 1 p = ‘hello’.index(X)
NameError: name ‘X’ is not defined
s1 = 'tracy is lucky girl'
s2 = s1.replace(' ','#')
print(s2)
tracy#is#lucky#girl
s1 = 'tracy is lucky girl'
s2 = s1.replace(' ','#',2)
print(s2)
# 可以指定替换次数
tracy#is#lucky girl
编码/解码函数:encode() 编码 , decode() 解码,
gbk 中文编码 , gb2312 简体中文编码
msg = '上课啦!认真听讲!'
result = msg.encode('utf-8')
print(result)
m = result.decode('utf-8')
print(m)
b’\xe4\xb8\x8a\xe8\xaf\xbe\xe5\x95\xa6\xef\xbc\x81\xe8\xae\xa4\xe7\x9c\x9f\xe5\x90\xac\xe8\xae\xb2\xef\xbc\x81’
上课啦!认真听讲!
字符串开始/结束:startswith(), endswith() 函数返回值为布尔类型
filename = '笔记.doc'
result = filename.endswith('.doc')
print(result)
True
s = 'hello'
result = s.startswith('he')
print(result)
True
# 应用举例:文件上传类型 只能上传图片(jpg,png,bmp,gif)
path = input('请选择文件:')
p = path.rfind('\\')
filename = path[p+1:]
print(p,filename)
if filename.endswith('jpg') or filename.endswith('png') or filename.endswith('bmp') or filename.endswith('gif'):
print('是图片允许上传')
else:
print('不是图片格式,只能上传图片!')
请选择文件:C:\user\desk\background.png
12 background.png
是图片允许上传
isalpha() 是否是字母 isdigit() 是否是数字
s = 'abcd'
result = s.isalpha()
print(result)
True
s = '686'
result = s.isdigit()
print(result)
True
# 应用举例:计算3个数字的累加结果
sum = 0
i = 1
while i <= 3:
num = input('请输入数字')
if num.isdigit():
num = int(num)
sum += num
print('第{}个数字累加成功!'.format(i))
i += 1
else:
print('不是数字!')
print('sum=',sum)
请输入数字ee
不是数字!
请输入数字3
第1个数字累加成功!
请输入数字4
第2个数字累加成功!
请输入数字56
第3个数字累加成功!
sum= 63
字符串内置函数:join()
new_str = '-'.join('abc')
print(new_str)
a-b-c
list1 = ['a','v','o','9']
result = ''.join(list1)
print(result)
avo9
字符串去空格函数:strip() , lstrip() , rstrip()
s = ' hello '
s = s.strip()
print(s+'Y')
helloY
s = ' hello '
s = s.lstrip()
print(s+'Y')
hello Y
s = ' hello world ; hello kitty '
s = s.strip()
print(s)
hello world ; hello kitty
分割字符串:split()
s = 'hello world hello kitty'
result = s.split(' ')
print(result)
n = s.count(' ')
print('个数',n)
[‘hello’, ‘world’, ‘hello’, ‘kitty’]
个数 3
s = 'hello world hello kitty'
result = s.split(' ',2)
print(result)
[‘hello’, ‘world’, ‘hello kitty’]