人生苦短,我學Python——【2】字符串、列表、元組、字典、集合

Python3 字符串html

【1】用引號( ' 或 " )來建立字符串,python訪問子字符串的值python

[] 索引字符串 api

[:]截取字符串中的一部分,遵循左閉右開原則,str[0:2] 是不包含第 3 個字符的。app

var1 = 'Hello World!' var2 = "Runoob"

print ("var1[0]: ", var1[0]) print ("var2[0:5]: ", var2[0:5]) print ("var2[:5]: ", var2[:5])

 輸出結果:函數

PS E:\Python> python test1.py var1[0]: H var2[0:5]: Runoo var2[:5]:  Runoo

 【2】字符串的拼接:+this

var1 = 'Hello World!' var2 = "Runoob" var3 = " " var4 = var1 + var3 + var2 print ("var4: ", var4)

 輸出結果:spa

PS E:\Python> python test1.py var4: Hello World! Runoob

 【3】字符串的格式化:.format()code

var1 = 10 var2 = ' ' var3 = f"There are{var2}{var1}{var2}people" var4 = "There are {} people"

print("There are",var1,"people") print ("var3: ", var3) print ("var4: ", var4.format(var1))

 輸出結果:orm

PS E:\Python> python test1.py There are 10 people var3: There are 10 people var4: There are 10 people

【4】字符串長度 lenhtm

var1 = "learn python"
len1 = len(var1)

print("字符串長度:", len1)

輸出結果:

PS E:\Python> python test1.py
字符串長度: 12

【5】in 成員運算符,若是字符串中包含給定的字符返回 True

  not in 成員運算符,若是字符串中不包含給定的字符返回 True

var1 = 'Hello World!' p1 = 'H' in var1 p2 = 'H' not in var1 print (p1) print (p2)

輸出結果:

PS E:\Python> python test1.py True False

 【6】字符串換行:"""\t\n

para_str = """這是一個多行字符串的實例 多行字符串能夠使用製表符 TAB ( \t )。 也能夠使用換行符 [ \n ]。 """
print(para_str)

輸出結果:

這是一個多行字符串的實例
多行字符串能夠使用製表符
TAB (    )。
也能夠使用換行符 [
 ]。

【7】內建函數:capitalize()將字符串轉換爲第一個字母大寫的字符串

var1 = "heLLo" var2 = var1.capitalize() print(var2)

輸出結果:

PS E:\Python> python test1.py Hello

 【8】內建函數:center()返回一個指定的寬度 width 居中的字符串,fillchar 爲填充的字符,fillchar 只能是單個字符,默認爲空格。

  width 小於字符串寬度直接返回字符串,不會截斷;

str = "[Learning Python]"

print (str.center(40, '*')) print (str.center(10, '*'))

輸出結果:

PS E:\Python> python test1.py
***********[Learning Python]************
[Learning Python]

【9】更多內建函數

 

Python3 列表

【1】建立一個列表,只要把逗號分隔的不一樣的數據項使用方括號[]括起來便可

列表的數據項不須要具備相同的類型

使用方括號索引來訪問列表中的值,一樣你也能夠截取字符

list1 = ['Learn', 'Python', 2018, 2019]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])

輸出結果:

list1[0]: Learn list2[1:5]:  [2, 3, 4, 5]

【2】列表元素的修改

list1 = ['Learn', 'Python', 2018, 2019]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; list1[3] = 2020
 
print (list1) print ("list2[1:5]: ", list2[1:5])

輸出結果:

PS E:\Python> python test1.py ['Learn', 'Python', 2018, 2020] list2[1:5]:  [2, 3, 4, 5]

【3】刪除列表元素del

list1 = ['Learn', 'Python', 2018, 2019];

print ("原始列表:", list1)
del list1[3]
print ("刪除元素後列表:", list1)

輸出結果:

原始列表: ['Learn', 'Python', 2018, 2019] 刪除元素後列表: ['Learn', 'Python', 2018]

【4】列表長度函數len(list)

list1 = ['Learn', 'Python', 2018, 2019]; len1 = len(list1) print ("原始列表:", list1) print ("原始列表長度:", len1) del list1[3] len1 = len(list1) print ("刪除元素後列表:", list1) print ("刪除元素後列表長度:", len1)

輸出結果:

原始列表: ['Learn', 'Python', 2018, 2019] 原始列表長度: 4 刪除元素後列表: ['Learn', 'Python', 2018] 刪除元素後列表長度: 3

【5】列表的拼接+

list1 = ['Learn', 'Python', 2018, 2019]; list2 = [1, 2, "ok"] list3 = list1 + list2 print ("拼接後列表:", list3)

輸出結果:

PS E:\Python> python test1.py 拼接後列表: ['Learn', 'Python', 2018, 2019, 1, 2, 'ok']

 【6】in 成員運算符,判斷元素是否存在於列表,返回布爾

list1 = ['Learn', 'Python', 2018, 2019]; list2 = [1, 2, "ok"] p1 = "Learn" in list1 p2 = "Learn" in list2 print (p1, p2)

輸出結果:

PS E:\Python> python test1.py True False

 【7】列表的嵌套

list1 = ["learn", "python"] list2 = ["I", "do"] list3 = [list1, list2] print("嵌套列表list3:", list3)

輸出結果:

PS E:\Python> python test1.py 嵌套列表list3: [['learn', 'python'], ['I', 'do']]

【8】在列表末尾添加新的對象方法list.append(obj)

list1 = ["learn", "python"] str1 = "good" list1.append(str1) print(list1)

輸出結果:

PS E:\Python> python test1.py ['learn', 'python', 'good']

【9】更多列表操做方法

 

 Python3 元組

【1】元組與列表相似,不過元組元素不能修改,但能夠拼接元組。元組中只包含一個元素時,須要在元素後面添加逗號,不然括號會被看成運算符使用

tup1 = ("learn", "python") str1 = tup1[1] print(str1)

輸出結果:

PS E:\Python> python test1.py python

 

 Python3 字典

【1】字典的每一個鍵值(key=>value)對用冒號(:)分割,每一個對之間用逗號(,)分割,整個字典包括在花括號{}

  字典的鍵必須是惟一的,但值能夠修改

dict = {'Name': 'Python', 'Age': 7, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age'])

輸出結果:

PS E:\Python> python test1.py dict['Name']: Python dict['Age']:  7

【2】修改和刪除字典元素

dict = {'Name': 'Python', 'Age': 7, 'Class': 'First'} print ("dict: ", dict) dict[1] = 1 dict[2] = 2 dict['Age'] = 18
del dict['Class'] print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age']) print ("dict: ", dict)

輸出結果:

PS E:\Python> python test1.py dict: {'Name': 'Python', 'Age': 7, 'Class': 'First'} dict['Name']: Python dict['Age']:  18 dict: {'Name': 'Python', 'Age': 18, 1: 1, 2: 2}

 【3】字典的更多騷操做

 

Python3 集合

【1】能夠使用大括號 { } 或者 set() 函數建立集合,注意:建立一個空集合必須用 set() 而不是 { },由於 { } 是用來建立一個空字典

thisset = set(("Google", "Python", "Taobao")) thisset.discard("School")# s.discard( x )移除元素即便元素不存在也不會發生錯誤
thisset.add("Facebook") thisset.remove("Google") print(thisset)

輸出結果:

PS E:\Python> python test1.py {'Python', 'Facebook', 'Taobao'}

【2】集合的更多騷操做

相關文章
相關標籤/搜索