Python学习:字符串
一、字符串創建
我們?般使?引號來創建字符串。創建字符串很簡單,只要為變量分配?個值即可。
a = 'hello world' print(type(a)) name1 = """ Rose """ name2= ''' Tom '''#三引號形式的字符串?持換?。二、字符串輸入輸出
(1)字符串輸出
name = 'Tom' print('我的名字是%s' % name)(2)字符串輸入
name = input('請輸?您的名字:') print(f'您輸?的名字是{name}') print(type(name))三、下標
“下標” ?叫 “索引” ,就是編號。下標從0開始
name = "abcdef" print(name[1]) print(name[0]) print(name[2])#輸出為b a c四、切片
切?是指對操作的對象截取其中?部分的操作。字符串、列表、元組都?持切?操作。
格式為:
序列[開始位置下標:結束位置下標:步?]
(1)不包含結束位置下標對應的數據, 正負整數均可;
(2) 步?是選取間隔,正負整數均可,默認步?為1
五、常用操作方法
(1)find():檢測某個?串是否包含在這個字符串中,如果在返回這個?串開始的位置下標,否則則返回-1。
str = "hello world and itcast and itheima and Python" print(str.find('and')) # 12(2)index():檢測某個?串是否包含在這個字符串中,如果包含則返回這個?串開始的位置下標,否則報異常。
str = "hello world and itcast and itheima and Python" print(str.index('and')) # 12 print(str.index('ands')) # 報錯(3)rfind(): 和find()功能相同,但查找?向為右側開始。
(4)rindex():和index()功能相同,但查找?向為右側開始。
(5)count():返回某個?串在字符串中出現的次數
(6)replace():替換,替換次數如果超出?串出現次數,則替換次數為該?串出現次數。
str = "hello world and itcast and itheima and Python" # 結果:hello world he itcast he itheima he Python print(str.replace('and', 'he')) # 結果:hello world he itcast he itheima he Python print(str.replace('and', 'he', 10)) # 結果:hello world and itcast and itheima and Python print(str)注:數據按照是否能直接修改分為可變類型和不可變類型兩種。字符串類型的數據修改的時候不能改變原有字符串,屬于不可變類型。
(7)split():按照指定字符分割字符串。
格式為:字符串序列.split(分割字符, num) ,
num表示的是分割字符出現的次數,即將來返回數據個數為num+1個。
(8)join():??個字符或?串合并字符串,即將多個字符串合并為?個新的字符串。
list = ['c', 'z', 'b', 'k'] t1 = ('a', 'b', 'c', 'd') # 結果:c_z_b_k print('_'.join(list)) # 結果:a...b...c...d print('...'.join(t1))(9)capitalize():將字符串第?個字符轉換成?寫。
str = "hello world " # 結果:Hello world print(str.capitalize())#capitalize()函數轉換后,只字符串第?個字符?寫,其他的字符全都?寫。(10)title():將字符串每個單詞?字?轉換成?寫。
str = "hello world " # 結果:Hello World print(str.title())(11)lower():將字符串中?寫轉?寫。
str = "Hello World" # 結果:hello world print(mystr.lower())(12)upper():將字符串中?寫轉?寫。
str = "hello world " # 結果:HELLO WORLD print(mystr.upper())(13)lstrip():刪除字符串左側空?字符。
(14)rstrip():刪除字符串右側空?字符。
(15)strip():刪除字符串兩側空?字符。
(16)ljust():返回?個原字符串左對?,并使?指定字符(默認空格)填充?對應?度 的新字符串。
格式為:
(17)rjust():返回?個原字符串右對?,并使?指定字符(默認空格)填充?對應?度 的新字符串,格式和ljust()相同。
(18)center():返回?個原字符串居中對?,并使?指定字符(默認空格)填充?對應?度 的新字符串,格式和ljust()相同。
(19)startswith():檢查字符串是否是以指定?串開頭,是則返回 True,否則返回 False。如果設置開始和結束位置下標,則在指定范圍內檢查。
格式為:
(20)endswith()::檢查字符串是否是以指定?串結尾,是則返回 True,否則返回 False。如果設置開始和結束位置下標,則在指定范圍內檢查。
格式為:
(21)isalpha():如果字符串?少有?個字符并且所有字符都是字?則返回 True, 否則返回 False。
mystr1 = 'hello' mystr2 = 'hello111' # 結果:True print(mystr1.isalpha()) # 結果:False print(mystr2.isalpha())(22)isdigit():如果字符串只包含數字則返回 True 否則返回 False。
mystr1 = 'a12' mystr2 = '12' # 結果: False print(mystr1.isdigit()) # 結果:False print(mystr2.isdigit())(23)isalnum():如果字符串?少有?個字符并且所有字符都是字?或數字則返 回 True,否則返回False。
str1 = 'aaa12345' str2 = '12345-' # 結果:True print(mystr1.isalnum()) # 結果:False print(mystr2.isalnum())(24)isspace():如果字符串中只包含空?,則返回 True,否則返回 False。
str1 = '1 2 3' str2 = ' ' # 結果:False print(str1.isspace()) # 結果:True print(str2.isspace())總結
以上是生活随笔為你收集整理的Python学习:字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习:列表
- 下一篇: Python学习:集合