赞
踩
- >>>7/2
- 3.5
- >>17//3
- 5
- >>>2**3
- 8
- str1 = "hello,world"
- str2 = "Jim"
- var1 = "hello world"
- var2 = "python3"
- print("var1[0]",var1[0] #var1[0] h
- print("var2[3:6]",var2[3:6]) #var2[3:6] hon
- str = "string"
- str[1:3] # "tr"
- #获取从偏移为1到偏移为3的字符串,不包括偏移为3的字符
- str[1:] # "tring"
- #获取从偏移为1到最后的一个字符,不包括最后一个字符
- str[:3] #"str"
- # 获取从偏移为0的字符一直到偏移为3的字符串,不包括偏移为3的字符串
- str[:-1] #strin"
- #获取从偏移为0的字符一直到最后一个字符(不包括最后一个字符串)
- str[:] #"string"
- #获取字符串从开始到结尾的所有元素
- str[-3:-1] #"in"
- #获取偏移为-3到偏移为-1的字符,不包括偏移为-1的字符
- str[::-1] # "gnirts"
- #反转输出
string = "hello"+"world" #"hello world"
字符串的方法
- str = "This is A Test"
- print(str.replace("is", "was")) # Thwas was A Test"
- print(str.replace("is", "was", 1)) #Thwas is A Test
str.find(str,beg = 0, end = len(str))
- str1 = "python web: www.python.org"
- str2 = "we"
- print(str1.find(str2)) # 7
- print(str1.find(str2, 3)) # 7
- print(str1.find(str2, 8)) # -1
②index()方法
str.join(sequence)
- str = "-"
- a = [1,2,4]
- b = ["a","b","c"]
- print(str.join(a)) #wrong
- print(str.join(b)) #a-b-c
str.split(str="", num=string.count(str)).
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
- str = "ithis is string examplei"
- print(str.split()) #['ithis', 'is', 'string', 'examplei']
- print(str.split("s",1)) #['ithi', ' is string examplei']
- print(str.split("i")) #['', 'th', 's ', 's str', 'ng example', '']
记住一点儿:str1.split(str2)按照 str2分割时候,就把str2 换成 "," 形式。以" i ipython oi" 为例,把字符串的所有 i 换成 ","形式,结果为:" ", " ","python o"," "
- str = "i love python"
- print(len(str)) #13
- List = [1,3,5,7]
- print(len(List)) #4
- str = "This Is A Test"
- print(str.lower()) # this is a test
- print(str.upper()) # THIS IS A TEST
- str = "tt44333"
- print(str.isnumeric()) #False
- str = "44333"
- print(str.isnumeric()) #True
- print(str.isspace()) #False
- str=" "
- print(str.isspace()) #True
- age = 10
- str = "python"
- print("今年%d岁"%age,"the string %s"%str)
- list1 = ["yqq", "boy", 27]
- list2 = [1, 2, 3, 4]
- list3 = ["a", "b", "c"]
- list1 = ["yqq", "boy", 27]
- list1[1] = "girl"
- print(list1) #['yqq', 'girl', 27]
- str = "4444"
- str[0]="5"
- print(str) #wrong
python列表的分片
格式:list[n, m]
n: 提取部分的第一个元素的编号。
- list = [1,2,3,4,5,6,7,8,9,10]
- print(list[3:6]) #[4,5,6]
- print(list[0:1]) #[1]
- print(list[7:10]) #[8,9,10]
- print(list[-3:]) #[8,9,10]
- print(list[-1:]) #[10] #分片操作
- print(list[-1]) #10 #访问列表的值
可以在分片时候,指定步长,步长可以为正数,也可以是负数。步长为负数,从右向左提取元素。
- list = [1,2,3,4,5,6,7,8,9,10]
- print(list[::2]) #[1, 3, 5, 7, 9]
- print(list[::-1]) #[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
- #从索引8到索引3,步长为 -1.
- print(list[8:3:-1]) #[9,8,7,6,5]
- print(list[0:10:-2]) #[]
- print(list[5::-2]) #[6, 4, 2]
- print(list[:5:-2]) #[10,8]
- >>>[1,2]+[3,4]
- [1,2,3,4]
- >>>[1,2,3]+"hello"
- wrong
序列迭代
- for x in [1, 2, 3]:
- print x,
- list1 = ['physics', 'chemistry', 1997, 2000];
- del list1[2]
- aTuple = (123, 'Google', 'Runoob', 'Taobao')
- list1 = list(aTuple)
- print ("列表元素 : ", list1) # 列表元素 : [123, 'Google', 'Runoob', 'Taobao']
- str="Hello World"
- list2=list(str)
- print ("列表元素 : ", list2) #列表元素 : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
python列表包含的函数
- List =[1,2,3,4]
- List1 =[3,4,5]
- List.append(5)
- print(List) #[1, 2, 3, 4, 5]
- List.append(List1)
- print(List)#[1, 2, 3, 4, 5, [3, 4, 5]]
list.extend(seq)
- List =[1,2,3,4]
- List1 =[3,4,5]
- List.extend(List1)
- print(List)#[1, 2, 3, 4, 5, 3, 4, 5]
- List.append(List1)
- print(List)#[1, 2, 3, 4, 5,[ 3, 4, 5]]
list.insert(index,obj)
- List =[1,2,3,4]
- List1 =[3,4,5]
- List.insert(3, List1)
- print(List)#[1, 2, 3, [3, 4, 5], 4]
list.pop()
- Tuple = () #创建元组
- Tuple = ("a","b","c","d")
- Tuple = ([1,2,3],"a",3,)
注意:一个元素的元组,后边加一个“,”。
Tuple =(30,)
访问元组
- >>> tuple = ("abcd",1,[1,2,4])
- >>> tuple[0]
- 'abcd'
- >>> tuple[1]
- 1
- >>> tuple[2]
- [1, 2, 4]
- >>>
修改元组
- >>> tuple = ("abcd",1,[1,2,4])
- >>> tuple[0]="ad" #wrong
元组合并- >>> tuple = ("abcd",1,[1,2,4])
- >>> tuple1 =("ad",3,4)
- >>> tuple + tuple1
- ('abcd', 1, [1, 2, 4], 'ad', 3, 4)
删除元组
- >>> tuple = ("abcd",1,[1,2,4])
- >>> del tuple
任意的以“,”分开的序列,默认是元组。
- >>> a = 1,2,3,4
- >>> a
- (1, 2, 3, 4)
- >>> a =[1,2,3,4],5,"str"
- >>> a
- ([1, 2, 3, 4], 5, 'str')
元组的函数
- dict = {"name":"yqq", "age":27}
- print(dict["name"],dict["age"])
- dict = {"name":"yqq", "age":27}
- dict["age"] =25
- print(dict) # {'name': 'yqq', 'age': 25}
- dict["school"] ="bj"
- print(dict) # {'name': 'yqq', 'school': 'bj', 'age': 25}
- del dict[key] #删除 key 条目
- del dict #删除字典
- dict.clear() #清除所有条目
字典的方法
- dict = {'name': 'yqq', 'school': 'bj', 'age': 25}
- print(len(dict)) #3
- print(dict.keys()) #dict_keys(['name', 'age', 'school'])
- print(dict.values()) #dict_values(['yqq', 25, 'bj'])
- print(dict.items()) #dict_items([('name', 'yqq'), ('age', 25), ('school', 'bj')])
字典的遍历
- for key in dict.keys():
- print(key)
- # name
- # age
- # school
- for value in dict.values():
- print(value)
- # yqq
- # 25
- # bj
- for item in dict.items():
- print(item)
- # ('name', 'yqq')
- # ('age', 25)
- # ('school', 'bj')
- for key,value in dict.items():
- print("key=%s"%key,"value=%s"%value)
- # key=name value=yqq
- # key=age value=25
- # key=school value=bj
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。