当前位置: 移动技术网 > IT编程>脚本编程>Python > 人生苦短,我学Python——【2】字符串、列表、元组、字典、集合

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

2018年12月29日  | 移动技术网IT编程  | 我要评论

牛图网,镜龙战,2016个人年度工作总结

python3 字符串

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

[] 索引字符串 

[:]截取字符串中的一部分,遵循左闭右开原则,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}

 

 

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】

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网