ptyhon之路day2

字符串 集合 列表 字典  元组 数字  数据类型 字符编码 文件处理 编码

一 字符串spa

什么是字符串?3d

用单引''号 或者双引号""引发来的就是字符串orm

字符串是 Python 中最经常使用的数据类型。咱们能够使用引号('或")来建立字符串blog

str()索引

例如:a=' ' 等于a=str('') 或者 a= ""   a=str("")ip

a="a b c d"字符串

print(a)it

 

 

常见用法字符编码

索引 移除空白strip() 切分 split() 取长度len() 

endswith('xx') 以xx结尾

startswith('xx') 以xx开头

replace('a','b',1) 替换 a替换b  第一个a

name='abc', name.find('a') ,name.find('a',0,2) 顾头不顾尾

join用法   

l=['h','e','l','l','o']

print(':'.join(l))  注意:拼接的内容必须都是字符串

format使用

print('{} {} {}'.format('name',age,'male'))

print('{0} {1} {0}'.format('name',age,'male'))

print('NAME:{name} AGE:{age} SEX:{sex}'.format(age=20,sex='male',name='zjw')

下标是0 的状况就打印第一个字符 

那么若是超过了会怎么样?  会提示 

集合 tuple()

Python 的元组与列表相似,不一样之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组建立很简单,只须要在括号中添加元素,并使用逗号隔开便可。

tup1 = (1, 2, 3, 4, 5 );
建立空元组

tup1 = ();
元组中只包含一个元素时,须要在元素后面添加逗号

tup1 = (50,);
元组与字符串相似,下标索引从0开始,能够进行截取,组合等。

访问元组

元组能够使用下标索引来访问元组中的值,以下实例:
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup2[1:5]: ", tup2[1:5])

删除元组

元组中的元素值是不容许删除的,但咱们能够使用del语句来删除整个元组

tup = ('Google', 'Runoob', 1997, 2000)

print (tup)
del tup;
print ("删除后的元组 tup : ")
print (tup)
以上实例元组被删除后,输出变量会有异常信息,输出以下所示:

删除后的元组 tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name 'tup' is not defined

元组运算符

与字符串同样,元组之间能够使用 + 号和 * 号进行运算。这就意味着他们能够组合和复制,运算后会生成一个新的元组。

相关文章
相关标签/搜索