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

歡迎訪問 生活随笔!

生活随笔

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

python

python基本数据类型——str

發布時間:2023/11/29 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python基本数据类型——str 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、字符串的創建

test = str() / "" test = str("licheng") / "licheng"
  • 無參數,創建空字符串
  • 一個參數,創建普通字符串
  • 兩個參數,int(字節,編碼)

二、字符串的常用方法

#capitalize():字符串首字符大寫 string = 'this is a string.' new_str = string.capitalize() print(new_str) #輸出:This is a string.#center(width, fillchar=None):將字符串放在中間,在指定長度下,首尾以指定字符填充 string = 'this is a string.' new_str = string.center(30,'*') print(new_str) #輸出:******this is a string.*******#count(sub, start=None, end=None):計算字符串中某字符的數量 string = 'this is a string.' new_str = string.count('i') print(new_str) #輸出:3#decode/encode(encoding=None, errors=None):解碼/解碼 string = 'this is a string.' new_str = string.decode() new_str = string.encode() print(new_str)#endswith(self, suffix, start=None, end=None):判斷是否以某字符結尾 string = 'this is a string.' new_str = string.endswith('ing.') print(new_str) #輸出:True#find(self, sub, start=None, end=None):在字符串中尋找指定字符的位置 string = 'this is a string.' new_str = string.find('a') #找的到的情況 print(new_str) #輸出:8 new_str = string.find('xx') #找不到的情況返回-1 print(new_str) #輸出:-1#index(self, sub, start=None, end=None):;類似find string = 'this is a string.' new_str = string.index('a') #找的到的情況 print(new_str) #輸出:8 new_str = string.index('xx') #找不到的情況,程序報錯 print(new_str) #輸出:程序運行報錯,ValueError: substring not found#isalnum(self):判斷字符串中是否都是數字和字母,如果是則返回True,否則返回False string = 'My name is yue,my age is 18.' new_str = string.isalnum() print(new_str) #輸出:False string = 'haha18121314lala' new_str = string.isalnum() print(new_str) #輸出:True#isalpha(self):判斷字符串中是否都是字母,如果是則返回True,否則返回False string = 'abcdefg' new_str = string.isalpha() print(new_str) #輸出:True string = 'my name is yue' new_str = string.isalpha() #字母中間帶空格、特殊字符都不行 print(new_str) #輸出:False# isdigit(self):判斷字符串中是否都是數字,如果是則返回True,否則返回False string = '1234567890' new_str = string.isdigit() print(new_str) #輸出:True string = 'haha123lala' new_str = string.isdigit() #中間帶空格、特殊字符都不行 print(new_str) #輸出:False# islower(self):判斷字符串中的字母是否都是小寫,如果是則返回True,否則返回False string = 'my name is yue,my age is 18.' new_str = string.islower() print(new_str) #輸出:True string = 'My name is Yue,my age is 18.' new_str = string.islower() print(new_str) #輸出:False# isupper(self):檢測字符串中所有的字母是否都為大寫。 string = 'MY NAME IS YUE.' new_str = string.isupper() print(new_str) #輸出:True string = 'My name is Yue.' new_str = string.isupper() print(new_str) #輸出:False# join(self, iterable):將序列中的元素以指定的字符連接生成一個新的字符串。 string = ("haha","lala","ohoh") str = "-" print(str.join(string)) #輸出:haha-lala-ohoh# lower(self):轉換字符串中所有大寫字符為小寫。 string = "My Name is YUE." print(string.lower()) # 輸出:my name is yue.# lstrip(self, chars=None):截掉字符串左邊的空格或指定字符。 string = " My Name is YUE." print(string.lstrip()) #輸出:My Name is YUE. string = "My Name is YUE." print(string.lstrip('My')) #輸出: Name is YUE.#replace(self, old, new, count=None):把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。 string = "My name is yue." print(string.replace("yue","ying")) #輸出:My name is ying.# rfind(self, sub, start=None, end=None):返回字符串最后一次出現的位置,如果沒有匹配項則返回-1。 string = "My name is yue." print(string.rfind('is')) #輸出:8 string = "My name is yue." print(string.rfind('XXX')) #輸出:-1# split(self, sep=None, maxsplit=None):通過指定分隔符對字符串進行切片。 string = "haha lala gege" print(string.split(' ')) #輸出:['haha', 'lala', 'gege'] print(string.split(' ', 1 )) #輸出: ['haha', 'lala gege']# rsplit(self, sep=None, maxsplit=None):通過指定分隔符對字符串從右進行切片。 string = "haha lala gege" print(string.rsplit(' ')) #輸出:['haha', 'lala', 'gege'] print(string.rsplit(' ', 1 )) #輸出: ['haha lala', 'gege']# rstrip(self, chars=None):刪除 string 字符串末尾的指定字符(默認為空格). string = " My name is yue. " print(string.rstrip()) #輸出: My name is yue.# strip(self, chars=None):移除字符串頭尾指定的字符(默認為空格)。 string = " My name is yue. " print(string.strip()) #輸出:My name is yue.# upper(self):將字符串中的小寫字母轉為大寫字母。 string = "my name is yue,my age is 18." print(string.upper()) #輸出:MY NAME IS YUE,MY AGE IS 18. ?str源碼

三、字符串的公共功能

  • 索引(只能取一個元素)
  • 切片(取多個元素)
  • 長度(len)
    • python2:按字節算長度
    • python3:按字符算長度
  • for循環(同長度的版本循環單位)

四、字符與字節的轉換

# 將gbk編碼的字符轉化為字節 s = "李程" b = bytes(s, encoding="gbk") type(b) 輸出為字節類型# 將字節轉化為字符 c = str(b, encoding="gbk")

五、字符串格式化

Python的字符串格式化有兩種方式:?百分號方式、format方式

百分號的方式相對來說比較老,而format方式則是比較先進的方式,企圖替換古老的方式,目前兩者并存。

1、百分號方式

%[(name)][flags][width].[precision]typecode ?參數詳解

常用格式化:

tpl = "i am %s" % "spark"tpl = "i am %s age %d" % ("spark", 18)tpl = "i am %(name)s age %(age)d" % {"name": "spark", "age": 18}tpl = "percent %.2f" % 99.97623tpl = "i am %(pp).2f" % {"pp": 123.425556, }tpl = "i am %.2f %%" % {"pp": 123.425556, }

2、Format方式

[[fill]align][sign][#][0][width][,][.precision][type] ?參數詳解

?常用格式化:

1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')2 3 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])4 5 tpl = "i am {0}, age {1}, really {0}".format("seven", 18)6 7 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])8 9 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 10 11 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 12 13 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 14 15 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) 16 17 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 18 19 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) 20 21 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) 22 23 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 24 25 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 26 27 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) 28 29 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

更多格式化操作:https://docs.python.org/3/library/string.html

轉載于:https://www.cnblogs.com/yechanglv/p/6935625.html

總結

以上是生活随笔為你收集整理的python基本数据类型——str的全部內容,希望文章能夠幫你解決所遇到的問題。

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