Python基礎之資料類型

來源:互聯網
上載者:User

標籤:ice   大小寫   []   ==   replace   反轉   隨機   對齊   apc   

列表

增:

name = []name.append()name.insert(index, element) #元素

刪:

name.pop(index) , default lastname.remove(element)del name[index]names.clear() #清空列表del names  刪除列表

改:

name[index] = NewValue #新的值names.extend(names2) #擴充names = names + names2 #擴充#names2.reverse() #反轉,names2.sort() #排序 ,是按ASCII表的順序

查:

name.index(element)  #返回index值name.count(element)name[index] #返回對應的值name #返回整個列表

 

集合
linux = {"alex","sb","aa"}python = {"alex","sb","bb"}

交集:

print(linux.intersection(python)) print(linux & python)  

差集:

print(linux.difference(python))  #差集,linux有,Python沒有的print(python.difference(linux)) #差集,python有,Linux沒有的print(linux-python)

並集:

print(linux.union(python)) print(linux|python)

反向差集,反向並集:

print(linux.symmetric_difference(python)) #對稱,差集,互相不在的都列印print(linux ^ python) #對稱,差集,互相不在的都列印

合并:

linux.update(python)  #把Python合并到linux集合中

增加:

linux.add("ALEX")

刪除:

linux.discard("alex") #刪除,跟remove一樣的,但是如果元素不存在不會報錯linux.pop() # 隨機刪除linux.remove(‘alex‘) #刪除,但是如果元素不存在會報錯

其他:

linux.difference_update(python) #求差集並賦值給Linux集合print(linux.issubset(python)) # 子集,linux裡有得,python裡是否都有print(linux.issuperset(python)) # 超集,父集print(linux.isdisjoint(python)) # 如果兩個子集,都沒有相同的,則返回為True

 

字串
name = "Alex eee Li"print(name.capitalize()) #首字母大寫print(name.casefold()) # 大寫變小寫 if choice == "Y" or choice == "y"print(name.center(50,‘-‘)) #長度多少,不夠得填充-print(name.count(‘e‘)) #統計字母出現的次數print(name.count(‘e‘,3,7)) #統計3到7 有幾個eprint(name.endswith("Li")) # 什麼結尾name = "Alex\teee Li"print(name.expandtabs(30)) #設定\t的長度print(name.find("e"))print(name.find("sdf",3)) #返回找到的第一個值的索引,找不到就返回-1
格式化輸出
print(‘a1a‘.isalnum()) #a-zA-Z 0-9print("10.1".isdecimal()) #是不是一個正整數print(‘aA‘.isalpha()) #是不是字母print(‘a‘.isidentifier()) #identifier 關鍵字,是不是合法的關鍵字,是不是合法的變數名。print(‘A‘.islower()) #是不是小寫print(‘A‘.isupper()) #是不是大寫print(‘a‘.isprintable()) #是不是可列印print(‘‘.isspace()) #是不是空格print(‘Today Headline‘.istitle()) #是不是英文標題li = [‘alex‘,‘jack‘,‘rain‘]print(‘:‘.join(li))   #把列表拼接成字串print(name.ljust(50,‘-‘)) #靠左對齊print(name.rjust(50,‘-‘)) #靠右對齊print(name.lower()) #大寫變小寫print(name.lstrip("Al")) #移除左邊什麼print(name.swapcase()) #大小寫互換name= "my name is {0}, i am {1} years old"IN = "abcde"OUT="[email protected]#$%"trans_table = str.maketrans(IN,OUT)print(name.translate(trans_table))   #字元翻譯print(name.replace(‘name‘,‘NAME‘,1)) #替換print(name.center(50,‘-‘))print(name.count("my"))name="Luchuan Gao"print(name.casefold())print(name.upper())

 

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.