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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符串类型变量的相关内置函数详解

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串类型变量的相关内置函数详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# isupper:判斷字符串是否全部為大寫,返回值為布爾類型,是:true;否:flase

# str='hello,world'

# print(str.isupper())

?

?

?

# isdigit:判斷字符串是否為整型變量,返回值為布爾類型,是:true;否:false

# str='hello,world'

# print(str.isdigit())

?

# upper:把字符串全部變為大寫

# str='hello,world'

# res=str.upper()

# print(res)

?

?

# islower:判斷字符串是否全部為小寫,返回值為布爾類型, 是:true;否:flase

# str='hello,world'

# print(str.islower())

?

?

# startswith:判斷字符串開頭是否為XX字符,(如果是判斷多個字符,則必須是連續的字符)

# 返回值為布爾類型,是:true;否:flase

# str='hello,world'

# print (str.startswith('hel'))

# print (str.startswith('el'))

?

?

# endswith:判斷字符串結尾是否為XX字符,(如果是判斷多個字符,則必須是連續的字符)

# 返回值為布爾類型,是:true;否:flase

# str='hello,world'

# print (str.endswith('hel'))

# print (str.endswith('ld'))

?

# index:索引指定字符,并取下標,從左往右搜索,到第一個字符為止,

# 編號從0開始,從0開始編號,空格等特殊字符也占一位

# 如果指定的字符不存在,會報錯

# str='hello,world'

# print(str.index('d'))

# print(str.index('l'))

?

# rindex:索引指定字符,并取下標,從右往左搜索,到第一個字符為止,

# 編號從左往右,從0開始編號,空格等特殊字符也占一位

# str='hello,world'

# print(str.rindex('o'))

# print(str.index('o'))

# print(str.rindex('a'))

?

?

# find:索引指定字符,并且返回下標,如果沒有該字符,則返回-1

# str='hello,world'

# print(str.find('e'))

# print(str.find('a'))

?

?

# istitle:判斷指定字符是不是title,返回值是布爾類型, 是:true;否:flase

# title是指單詞首字母為大寫

# str='hello,world'

# print(str.istitle())

?

?

# title:將指定字符變為title

# str='hello,world'

# print(str.title())

?

?

?

?

# isalpha:判斷字符是否為英文或者漢字,返回值為布爾類型, 是:true;否:flase

# str='hello world'

# print(str.isalpha())

# str='helloworld你好世界'

# print(str.isalpha())

?

?

?

# count:統計字符串中指定字符出現的個數,返回值為布爾類型,是:true;否:flase

# 如果字符串中出現空格 特殊字符,則返回值為flase

# str='hello world'

# print(str.count('o'))

?

?

# isspace:判斷字符串是否為空格,返回值為布爾類型,是:true;否:flase

# str='hello world'

# print(str.isspace())

# str=' ?????'

# print(str.isspace())

?

?

# isalnum:判斷字符串中是否只包含字符,返回值為布爾類型,是:true;否:flase

# str='hello world'

# print(str.isalnum())

# str='helloworld'

# print(str.isalnum())

?

?

?

# replace:替換字符串中的指定字符,從左往右替換

# replace('舊字符','新字符',替換次數)

# str='hello world'

# print(str.replace('o','a',1))#只修改一次

# print(str.replace('o','a',2))#修改兩次

# print(str.replace('o','a'))#不指定修改次數,默認全部修改

# print(str.replace('o','a',3))#次數超過字符的真實個數,全部修改

?

?

?

#join: 把一個迭代對象變成字符串,該迭代對象中的元素必須是字符

# 可迭代對象包括 ?集合,元組,列表,字典,字符串

# res = ''.join(['a','b','a'])

# print(['a','b','a'])

# print(res)

?

?

# split:把一個字符串變成列表,

# split('指定的分隔符',分割次數)

# str='hello,world,hello,world!'

# print(str.split(',',2))

# print(str.rsplit(',',2))

?

?

?

?

# strip:去除字符串左右兩端指定字符

# str='hello,world,hello,world,hello'

# print(str.strip('hello'))

# lstrip:去除左端指定字符

# str='hello,world,hello,world,hello'

# print(str.lstrip('hello'))

# rstrip:去除右端指定字符

# str='hello,world,hello,world,hello'

# print(str.rstrip('hello'))

?

?

?

#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)

?

?

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

# str1 = 'Hello,World'

# print(str1[-100])

?

?

#字符串的拼接

# str1 = 'Hello,World'

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

# print('1'+'2')

?

?

#切片

# str1 = 'Hello,World'

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

# print(res)

?

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

# print(res)

?

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

# print(res)

?

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

# print(res)

?

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

# print(res)

?

?

#三引號和雙引號和單引號任意切換,交替使用

# str1 = ''' "what's your name????" '''

# print(str1)

?

# str1 = "what's your name????"

# print(str1)

?

?

轉載于:https://www.cnblogs.com/leeeel/p/10815445.html

總結

以上是生活随笔為你收集整理的字符串类型变量的相关内置函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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