Python3 字符串

【1】用引号( ' 或 " )来创建字符串,python访问子字符串的值

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

[] 索引字符串 

[:]截取字符串中的一部分,遵循左闭右开原则,str[0:2] 是不包含第 3 个字符的。

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】字符串的拼接:+

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

 输出结果:

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

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

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))

 输出结果:

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

【4】字符串长度 len

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】集合的更多骚操作

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄