1.isupper判断字符串是否全部都是大写
#eg:
str1 = 'Hello,world'
str2 = 'HELLO,WORLD'
res = str2.isupper()
print(res)
True
2.isalnum判断字符串里是否是数字或字母不能有其他字符
#eg:
str1 = '11122333aaa'
res = str1.isalnum()
print(res)
True
3.isdigit判断字符串里面是否是整型
#eg:
str1 = '123'
print(str1.isdigit())
True
#eg:
str1 = 'Hello,world'
print(str1.isdigit())
False
4.upper()方法把字符串全部变成大写
#eg:
str1 = 'Hello,world'
print(str1.upper())
HELLO,WORLD
5.islower判断字符串是否为小写
#eg:
str1 = 'Hello,world'
str1.islower()
False
6.startswith判断字符串开头是否为He
str1 = 'Hello,world'
print(str1.startswith('He'))
True
7.endswith判断字符串结尾是否为ld
#eg:
str1 = 'Hello,world'
print(str1.endswith('ld'))
True
8.index取字符串o的下标,如果没有这个字符会报错
rindex是从右往左检索但标记的时候依然是从左往右标记
#eg:
str1 = 'Hello,world'
print(str1.index('o'))
9.find取字符串o的下标,如果没有这个字符返回-1
str1 = 'Hello,world'
print(str1.find('o'))
10.isalpha判断字符串里是否全部都是英文
#eg:
str1 = 'Hello,world'
print(str1.isalpha())
False
#eg:
str1 = 'Helloworld'
print(str1.isalpha())
True
11.count统计字符串里l字符的个数
#eg:
str1 = 'Hello,world'
print(str1.count('l'))
3
12.istitle判断是否是抬头
#eg:
str1 = 'Hello,world'
print(str1.istitle())
False
#eg:
str1 = 'Hello ,World'
print(str1.istitle())
True
13.把一个字符串变成抬头
#eg:
str1 = 'Hello,world'
print(str1.title())
Hello,World
14.isspace判断字符串是否是纯空格
#eg:
str1 = ' '
print(str1.isspace())
True
#eg:
str1 = ''
print(str1.isspace())
False
#需要必须掌握的
15.replace替换字符串o成sb,并且只替换1次
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb',1)
print(res)
Hellsb,world
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb',2)
print(res)
Hellsb,wsbrld
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb')
print(res)
Hellsb,wsbrld
16.把一个可迭代对象(列表,元组,集合,字典,字符串)变成字符串
#eg:
res = ''.join('aaa')
print(res)
print(type(res))
aaa
eg:
res='a'.join(['1','2'])
print(res)
1a2
17.把一个字符串从左往右切分变成列表(.代表切分点,1代表切分1次)
#eg:
str1 = '192.168.160.132'
res = str1.split('.',1)
print(res)
['192', '168.160.132']
#eg:
str1 = '192.168.160.132'
res = str1.split('.',2)
print(res)
['192', '168', '160.132']
#eg:
str1 = '192.168.160.132'
res = str1.split('.')
print(res)
['192', '168', '160', '132']
18.把一个字符串从右往左切分变成列表(.代表切分点,1代表切分1次)
#eg:
str1 = '192.168.160.132'
res = str1.rsplit('.',1)
print(res)
['192.168.160', '132']
19.去除字符串左右两边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.strip('=')
print(res.strip('+'))
Hello,world
20.去除字符串右边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.rstrip('=')
print(res)
++++++Hello,world
21.去除字符串左边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.lstrip('+')
print(res)
Hello,world=====
22.format将字符串格式化,可以有以下3种格式
#eg:
str1 = 'my name is {},my age is {}'
res = str1.format('吉喆', '23')
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is {1},my age is {0}'
res = str1.format('23', '李凯')
print(res)
my name is 李凯,my age is 23
#eg:
str1 = 'my name is {name},my age is {age}'
res = str1.format(name='李凯', age='23')
print(res)
my name is 李凯,my age is 23
23.%s,%d,%f可以格式化字符串
%d只可以接收整数,%s可以接收数字也可以接收字符串%f接受浮点型
#eg:
str1 = 'my name is %s, my age is %d'
res = str1 % ('吉喆', 23)
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is %s, my age is %s'
res = str1 % ('吉喆', 23)
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is %s, my age is %d'
res = str1 % ('吉喆', ‘23’)
print(res)
错误
24.利用索引或者下标取值,超出范围报错从前往后是0 1 2 3......
从后往前是-1 -2 -3 -4 .......
#eg:
str1 = 'Hello,World'
print(str1[-1])
d
#eg:
str1 = 'Hello,World'
print(str1[1])
e
25.字符串的拼接
#eg:
print(str1[4]+str1[5])
o,
26.切片
#eg:
str1 = 'Hello,World'
res = str1[2:5]#正向切片顾头不顾尾
print(res)
llo
#eg:
str1 = 'Hello,World'
res = str1[-4:-1]#反向也是顾头不顾尾
print(res)
orl
#eg:
str1 = 'Hello,World'
res = str1[:3]#索引为3往右的字符不要了(包括下标为3的字符)
print(res)
Hel
#eg:
str1 = 'Hello,World'
res = str1[3:]#索引为3往左的字符不要了(不包括下标为3的字符)
# print(res)
lo,world
#eg:
str1 = 'Hello,World'
res = str1[::2]#步长为2,隔一个字符取一个字符
print(res)
Hlowrd
27.三引号和双引号和单引号任意切换
#eg:
str1 = '''
"what's your name????"
'''
print(str1)
"what's your name????"
有单引号可以用双引号括,有双引号可以用三引号括,有三引号可以用单引号括