4.python编程之——字符串详解

字符串详解用法

1 str.isupper()判断字符串是否全为大写
str1 = 'Hello,World'
print(str.isupper(str1))
输出:
False
2 str.isdigit 判断字符串里面是否为整数
str1 = 'Hello,World'
print(str.isdigit(str1))
输出:
False
3 str.upper()方法把字符串全部变成大写
str1 = 'Hello,World'
print(str.upper(str1))
输出:
HELLO,WORLD
4 str.islower判断字符串是否为小写
str1 = 'Hello,World'
print(str.islower(str1))
输出:
False
5 startswith判断字符串开头是否为’He’
str1 = 'Hello,World'
print(str1.startswith('He'))
输出:
True
6 endswith判断字符串结尾是否为’ld’
str1 = 'Hello,World'
print(str1.endswith('ld'))
输出:
True
7 index取字符串o的下标,如果没有这个字符会报错
str1 = 'Hello,World'
print(str1.index('o'))
print(str1.index('p'))
输出:
4
print(str1.index('p'))
ValueError: substring not found
8 find取字符串o的下标,如果没有这个字符串赶回-1
str1 = 'Hello,World'
print(str1.find('o'))
print(str1.find('p'))
 输出:
 4
 -1
9 isalpha判断字符串里是否都是全英文
str1 = 'Hello,World'
print(str1.isalpha())
输出:
False
str1 = 'HelloWorld'
print(str1.isalpha())
输出:
True
10 count统计字符串里l字符的个数
str1 = 'Hello,World'
print(str1.count('l'))
输出:
3
11 istitle判断是否是抬头
str1 = 'Hello,World'
print(str1.istitle())
输出:
True
12 把一个字符串变成抬头str.title
str1 = 'hello,world'
print(str1.title())
输出:
Hello,World
13 isspace判断字符串是否是纯空格
str1 = 'hello,world'
print(str1.isspace())
输出:
False

str1 = '   '
print(str1.isspace())
输出:
True
14 replace替换字符串
str1 = 'hello,world'
res = str1.replace('h','H')    #h全部替换为H
print(res)
输出:
Hello,world
str1 = 'Hello,World'
res = str1.replace('l','a',1)    #替换一次
print(res)
输出:
Healo,World
15 join把一个可迭代对象(列表、元组、集合、字典、字符串)变成字符串
res = ''.join(('1','f','2','5.1'))
print(res)
输出:
1f25.1

res = ''.join({'name':'gouzi','age':'18'})
print(res)      #字典遍历的是key
输出:
nameage
16 split把一个字符串从左往右切分变成列表(.代表切分点,1代表切分次数)
str1 = '192.168.1.150'
res = str1.split('.',1)
print(res)
输出:
['192', '168.1.150']
17 rsplit把一个字符串从右往左切分成列表(.代表切分点,1代表切分次数)
str1 = '192.168.1.150'
res = str1.rsplit('.',1)
print(res)
输出:
['192.168.1', '150']
18 strip()去除字符串左右两边特定的字符
str1 = '     Hello,World     '
res = str1.strip(' ')
print(res)
输出:Hello,World
19 rstrip()去除右边特定字符
str1 = '+++Hello,World+++'
res = str1.rstrip('+')
print(res)
输出:
+++Hello,World
20 lstrip()去除左边特定字符
str1 = '+++Hello,World+++'
res = str1.lstrip('+')
print(res)
输出:
Hello,World+++
21 format字符串格式化

方式1:

str1 = 'my name is {},my age is {}'
res = str1.format('bob','18')
print(res)
输出:
my name is bob,my age is 18

方式2:

str1 = 'my name is {1},my age is {0}'
res = str1.format('18','bob')
print(res)
输出:
my name is bob,my age is 18

方式3:

str1 = 'my name is {name},my age is {age}'
res = str1.format(name='bob',age='18')
print(res)
输出:
my name is bob,my age is 18
22 isalnum判断字符串里是否有数字或字符,有特殊字符为false
str1 = '23df'
print(str1.isalnum())
输出:
True

str1 = '23df,'
print(str1.isalnum())
输出:
False
23 %s,%d,%f可以格式化字符串
str1 = 'my name is %s,my age is %s'
res = str1%('bob','18')
print(res)
输出:
my name is bob,my age is 18
24 利用索引或下标取值,超出范围报错
str1 = 'Hello,World'
print(str1[0])    #字符串从左取下标由0开始
print(str1[-5])   #从右往左去下标由-1开始
输出:
H
W
25 字符串的拼接

先取字符再拼接

str1 = 'Hello,World'
print (str1[4]+str1[6])
输出:
oW
26 字符串切片

正向切片顾头不顾尾 str1[2:5]

str1 = 'Hello,World'
print(str1[2:5])
输出:
llo

反向切片顾尾不顾头

str1 = 'Hello,World'
print(str1[-4:-1])
输出:
orl

str1[:3]索引为3往右的字符丢弃(包括下标为3的字符)

str1 = 'Hello,World'
print(str1[:3])
输出:
Hel

str1[3:]索引为3往左的字符丢弃(包括下标为3的字符)

str1 = 'Hello,World'
print(str1[3:])
输出:
lo,World

str1[::2]步长为2,隔一个字符取一个字符

str1 = 'Hello,World'
print(str1[::2])
输出:
HloWrd

猜你喜欢

转载自blog.csdn.net/rzlongg/article/details/89419908