一.字符串
name="python" #单引号
name="python" #双引号\n
#换行符\t
#横向制表符\r
#回车\v
#纵向制表符print('今天是%s年%s月%s日'%(2021,22,21))
print('今天是{}月{}日'.format(11,24))
print('今天是%{0}s年%{1}s月%{2}s日'%{'0':2021,'1':11,'2':24})
msg = '我叫%s,今年%d,我的学习进度1%%' % ('关亮和', 28)
print(msg)
name=input('请输入你的名字:')
today='hello wrold!'
print(today.find('w'))
#索引默认从0开始,空格占用一个字符6
2.index #通过元素找索引,找到第一个就返回,没有就报错。
today='hello wrold!'
print(today.index('e'))
1
3.upper #返回大写字符串
today='hello wrold!'
print(today.upper())
HELLO WROLD!
4.lowwer #返回小写字符串
today='hello wrold!'
print(today.lower())
hello wrold!
5.startswitch #判断字符串是否以指定字符串开头
today='hello wrold!'
print(today.startswith('h'),type(today.startswith('h')))
True <class 'bool'> #返回的数据类型为bool类型
6.endswitch #判断字符串是否以指定字符串结尾
today='hello wrold!'
print(today.endswith('d'))
False
7.splite #指定字符分隔符
fruit='apple,peach,banana'
list=fruit.split('_')
print(list)
#返回的数据类型为列表
['apple,peach,banana']
8.strip #去字符串函数,
s=' abc '
print(s.strip())#默认删除空白字符串 abc
name='chensir'print(name.strip('chen'))
#删除指定字符 sir 9.replace #用来查找替换
name='chensir'
print(name.replace('chen','li'))
lisir
10.count #统计当前字符在字符串中出现的次数
s='safhjjjjjksdf'
print(s.count('j'))
5
11.title #每个单词的首字母大写
name='chensir,lisir'
print(name.title())
Chensir,Lisir
12.center #字符串居中前后填充的自定义字符
today='hello word!'
print(today.center(20)) #居中对齐,以空格进行填充 print(today.center(20,'='))
#居中对齐,以=号进行填充
hello word!
hello word!=
today='hello word!'
print(today.ljust(10,'='))
#左对齐
print(today.rjust(10,'='))
#右对齐
13.swapcase #大小写翻转
name='CHENsir'
print(name.swapcase())
chenSIR
14.join #自定制连接符,将可迭代对象中的元素连接起来
s='a,b,c'
print('_'.join(s))
a_,b,_c
15.capitalize #字符串首字母大写
name='chensir'
print(name.capitalize())
Chensir
5.字符串运算符
| 操作符 | 描述 |实例 |
|+ | 字符串连接 | a='hello' b='world' a+b='hello world'|
| * |重复输出字符串 | a*2='hellohello' |
| in | 测试一个字符在另一个字符串中 | 'h' in a |
| not in |测试一个字符不在另一个字符串中 |'h' not in a |