日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串介绍_python字符串详解

發布時間:2023/12/20 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串介绍_python字符串详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

必選掌握

#isupper判斷字符串是否全部都是大寫

str1 = 'Hello,world'

str2 = 'HELLO,WORLD'

print(str1.isupper()) False

print(str2.isupper()) True

#islower判斷字符串是否全部都是小寫

str1 = 'Hello,world'

str2 = 'hello,world'

print(str1.islower()) False

print(str2.islower()) True

#isalnum判斷字符串里是否有數字、字母、漢字.(或判斷字符串里是否有特殊字符)

str1 = '11122333aaah郝'

str2 = '11122333/qaaa'

print(str1.isalnum()) True

print(str2.isalnum()) false

#isdigit判斷字符串里面是否是整型

str1 = '123'

print(str1.isdigit())

True

#upper()方法把字符串全部變成大寫

str = 'Hello,World'

print(str.upper())

HELLO,WORLD

#startswith判斷字符串開頭是否為He

print(str1.startswith('He'))

#endswith判斷字符串結尾是否為ld

print(str1.endswith('ld'))

#index取字符串o的下標,如果沒有這個字符會報錯

str1 = 'Hello,World'

print(str1.index('o'))

4

print(str1.rindex('o'))

7

#find取字符串o的下標,如果沒有這個字符返回-1

str1 = 'Hello,World'

print(str1.find('o'))

4

print(str1.rfind('o'))

7

print(str1.find('y'))

-1

#isalpha判斷字符串里是英文或漢字

str1 = 'Hello,World'

print(str1.isalpha())

false

#count統計字符串里l字符的個數

print(str1.count('l'))

3

#istitle判斷是否是抬頭

抬頭判斷標準:連續不間斷單詞首字母大寫,其余為小寫,否則為false

print(str1.istitle())

True

#把一個字符串變成抬頭

print(str1.title())

#isspace判斷字符串是否是純空格

str1 = ''"

str2 = '' "

print(str1.isspace())

false

print(str2.isspace())

Ture

# replace替換字符串o成sb,并且只替換1次

str1 = 'Hello,world'

res = str1.replace('o','sb',1)

print(res)

Hellsb,world

#把一個可迭代對象(列表,元組,集合,字典,字符串)變成字符串,表中數據類型為字符串

res = ''.join(['abc','123','吉喆'])

print(res)

print(type(res))

abc123吉喆

# str1 = '192.168.250.250'

把一個字符串從左往右切分變成列表(.代表切分點,1代表切分1次)

res = str1.split('.',1)

print(res)

['192', '168.250.250']

#把一個字符串從右往左切分變成列表(.代表切分點,1代表切分1次)

res = str1.rsplit('.',1)

print(res)

['192.168.250', '250']

#去除字符串左右兩邊指定的字符(注意:必須為兩邊邊界)

str1 = '++++++Hello,World====='

res = str1.strip('=')

print(res)

++++++Hello,World

print(str1.strip('+'))

Hello,World=====

#去除字符串右邊指定的字符

res = str1.rstrip('=')

print(res)

#去除字符串左邊指定的字符

str1.lstrip('+')

#format將字符串格式化,可以有以下3種格式

str1 = 'my name is {},my age is {}'

res = str1.format('吉喆', '23')

str1 = 'my name is {1},my age is {0}'

res = str1.format('23', '李凱')

str1 = 'my name is {name},my age is {age}'

res = str1.format(name='李凱', age='23')

print(res)

#%s,%d,%f可以格式化字符串

str1 = 'my name is %s, my age is %s'

res = str1 % ('吉喆', 23)

print(res)

my name. is 吉喆, my age is 23

#利用索引或者下標取值,超出范圍報錯

str1 = 'Hello,World'

print(str1[-100])

#字符串的拼接

print(str1[4]+str1[5])

o,

print('1'+'2')

12

#切片

str1 = 'Hello,World'

res = str1[2:5]#正向切片顧頭不顧尾

print(res)

llo

# res = str1[-4:-1]#顧尾不顧頭

print(res)

rld

# res = str1[:3]#索引為3往右的字符不要了(包括下標為3的字符)

print(res)

Hel

# res = str1[3:]#索引為3往左的字符不要了(不包括下標為3的字符)

print(res)

lo,World

# res = str1[::2]#步長為2,隔一個字符取一個字符

print(res)

HloWrd

總結

以上是生活随笔為你收集整理的python字符串介绍_python字符串详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。