python基礎三

來源:互聯網
上載者:User

標籤:串連符   bst   value   order   spring   spl   string   實現   --   

資料類型之間的轉化int<-->str
int轉str:str(digit)str轉int:int(string) 注意此處的string只能是包含數位字串或開頭有負號
int<-->bool
bool(非零)-->Truebool(零)  -->Falseint(True) -->1int(False)-->0
bool<-->str
bool -->str沒有實際意義str  -->bool:bool(非Null 字元串) --> Truebool(Null 字元串)   --> False注意:Null 字元串為'',而不是' '。
python的字串(string)的學習第一部分:索引切片步長按照索引取值,取出來的都是一個字元,形成的字串。
>>> str1 = 'python'>>> str1[2]'t'>>> str1[-1]'n'
按切片取值,顧頭不顧尾
>>> str2 = 'python'>>> str2[0 : -1]'pytho'>>> str2[0:]'python'
按照切片+步長
>>> str3 = 'python is a language'>>> str3[::2]'pto salnug'>>> str3[::-1]'egaugnal a si nohtyp'當步長為負時,是倒著取值。

另外可以藉助數組來實現把字串倒序

strA = input("請輸入需要翻轉的字串:")order = [] for i in strA:  order.append(i)order.reverse()   #將列表反轉print ''.join(order)    #將list轉換成字串
第二部分:字串的常用方法。capitalize() 首字母大寫 ★★☆☆☆
>>> str4 = 'hellokitty'>>> str4.capitalize()'Hellokitty'
center() 字串置中前後填充自訂的字元 ★★☆☆☆
>>> str4 = 'hellokitty'>>> str4.center(20, '*')'*****hellokitty*****'
upper()英文全大寫;lower()英文全小寫。 在驗證碼驗證上會使用到。 ★★★★★
>>> str4 = 'hello kitty'>>> str4.upper()'HELLO KITTY'>>> str5 = 'Hello Kitty'>>> str5.lower()'hello kitty'
swapcase() 大小寫翻轉 ★★☆☆☆
>>> str5 = 'Hello Kitty'>>> str5.swapcase()'hELLO kITTY'
title() 非字母隔開的每個部分的首字母大寫 ★★☆☆☆
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.title()'Hello Kitty,What Can I Do4You?'
startswith()以什麼弄著 endswith()以什麼結尾 ★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.startswith('h')True>>> str6.startswith('hello')True
find 通過元素找索引,找到第一個就返回,沒有此元素則返回-1 ★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.find('h')0>>> str6.find('what')12>>> str6.find('.')-1
index 通過元素找索引,找到第一個就返回,沒有此元素則報錯 ★★★★★
>>> str6 = 'hello kitty,what can I do4you?'>>> str6.index('h')0>>> str6.index('what')12>>> str6.index('.')Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found
strip 預設去除字串前後的空格,分行符號,定位字元 ★★★★★
>>> str7 = "\tI'm a superman. ">>> str7.strip()"I'm a superman.">>> str7 = '==hey guys, you shutup!=='>>> str7.strip('==')'hey guys, you shutup!'
lstrip() rstrip()
lstrip()是去除左邊的。rstrip()是去除右邊的。
split() 將字串分割成列表(str---> list),預設以空格分割,可指定字元。 ★★★★★
>>> str8 = 'spring summer autumn winter'>>> str8.split()['spring', 'summer', 'autumn', 'winter']>>> str8 = 'spring,summer,autumn,winter'>>> str8.split(',')['spring', 'summer', 'autumn', 'winter']
split面試題 ★★★★★
一般有n個分割符,就有n+1個列表元素。但有個特例:當字串開頭有一個空格時並不指定分割符,就只會有n個列表元素。>>> str8 = ' spring summer autumn winter'>>> str8.split()['spring', 'summer', 'autumn', 'winter']>>> str8.split(' ')['', 'spring', 'summer', 'autumn', 'winter']
split([sep [, maxsplit]])可設定分割次數。
>>> str8.split(',', 1)['spring', 'summer,autumn,winter']可以用在分割路徑與檔案名稱:  ★★★★★>>> file_path = r'E:\aaa\bbb\ccc.ddd'>>> path, file = file_path.rsplit('\\', 1)>>> path'E:\\aaa\\bbb'>>> file'ccc.ddd'
join 自定製串連符,將可迭代對象中的元素串連起來 ★★★★★
>>> s1 = 'abc'>>> '_'.join(s1)'a_b_c'
replace 替換字串,可以指定替換次數 ★★★★★
>>> s2 = "Jack is Mary's boy friend.">>> s2.replace('Jack', 'Mark')"Mark is Mary's boy friend.">>> s2.replace('a', 'o', 1)"Jock is Mary's boy friend."
格式化輸出:format
s1 = '我叫{},今年{},性別{}'三種方式第一種s2 = '我叫{},今年{},性別{}'.format('太白','28','男')print(s2)第二種s3 = '我叫{0},今年{1},性別{2},我依然叫{0}'.format('太白', '28', '男')print(s3)第三種s4 = '我叫{name},今年{age},性別{sex}'.format(age='28', name='太白', sex='男')print(s4)
is 系列
name = 'taibai'name1 = 'a123'print(name.isalnum())  # 數字或字母組成print(name1.isdigit())  # 判斷全部是由整數組成print(name.isalpha())  # 全部由字母組成
公用方法
>>> name='alexaaaa'>>> name.count('a')  #可以設定起始位置,結束位置。5>>> print(len(name))8

python基礎三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.