python---字符串详解
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????"
有單引號可以用雙引號括,有雙引號可以用三引號括,有三引號可以用單引號括
總結
以上是生活随笔為你收集整理的python---字符串详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue绑定事件、vue双向绑定 2022
- 下一篇: Python反转字典的两种方法