六、字符串格式化--------列表常用操作

  • 移除空白  strip
  • 分割    split
  • 长度    len(xxx)
  • 索引    obj[1]
  • 切片    obj[1:]   obj[1:10]  obj[-1:]

字符串格式化

  • 问年龄
  • 问性别
  • 问工作
  • 问完之后统一打印出一个格式化的形式
 1 # -*- coding:utf-8 -*-
 2 name = input("name:")
 3 age = input("age:")
 4 job = input("job:")
 5 #此处看着特别迷糊,可以考虑其他办法
 6 #此种写法为拼接形式,每添加一个字符串都会在内存里单独开辟一个空间,相当于创建一个新的变量
 7 print("Infomation of :" +name+ "
Name:" +name+ "
Age:" +age+ "
Job:"+job)
 8 #此种写法在内存中一共只有一个空间,正常来说不建议通过拼接的形式来做
 9 print("Information of %s:
Name:%s
Age:%s
Job:%s" %(name,name,age,job))
10 #以上两种打印方式全部写在一行里如果数据量较大操作起来比较乱
11 #Python中单引号与双引号没有区别
12 #第三种方法
13 asg = '''
14 Infomation of %s:
15 Name:%s
16 age:%s
17 job:%s
18 ''' %(name,name,age,job)
19 print(asg)
20 
21 注意:python中的+号,数字只能加数字,字符串智能加字符串
22 print("Infomation of :" +name+ "
Name:" +name+ "
Age:" +age+ "
Job:"+job)

看以下错误:

  • python中的+号,数字只能加数字,字符串智能加字符串
  • age已经是数字了就不能通过加号的形式跟字符串相加了
  • 数字只能加数字
  • 字符串只能加字符串
 1 # -*- coding:utf-8 -*-
 2 name = input("name:")
 3 age = int(input("age:"))
 4 job = input("job:")
 5 print("Infomation of :" +name+ "
Name:" +name+ "
Age:" +age+ "
Job:"+job)
 6 --------------------------------------------------------------------------------
 7 name:aa
 8 age:33
 9 job:aa
10 Traceback (most recent call last):
11   File "F:/python/day1/字符串格式化.py", line 8, in <module>
12     print("Infomation of :" +name+ "
Name:" +name+ "
Age:" +age+ "
Job:"+job)
13 TypeError: Can't convert 'int' object to str implicitly

# 字符串是 %s;整数 %d;浮点数%f

 1 asg = '''
 2 Infomation of %s:
 3 Name:%s
 4 age:%d
 5 job:%s
 6 ''' %(name,name,age,job)
 7 print(asg)
 8 ------------------------------------------------------------------------------------
 9 name:aa
10 2age:2
11 job:dd
12 Traceback (most recent call last):
13   File "F:/python/day1/字符串格式化.py", line 19, in <module>
14     ''' %(name,name,age,job)
15 TypeError: %d format: a number is required, not str
16 
17 Process finished with exit code 1

错误中提到需要number 但是你却给了一个不是字符串

在这种情况下可以这样写

 1 # -*- coding:utf-8 -*-
 2 name = input("name:")
 3 age = int(input("age:"))
 4 job = input("job:")
 5 asg = '''
 6 Infomation of %s:
 7 Name:%s
 8 age:%d
 9 job:%s
10 ''' %(name,name,age,job)
11 print(asg)
12 ------------------------------------------------------------------------------------
13 name:aa
14 age:21
15 job:dd
16 
17 Infomation of aa:
18 Name:aa
19 age:21
20 job:dd
  •  移除空白 xxx.strip()
 1 # -*- coding:utf-8 -*-
 2 name = input("name:")
 3 age = int(input("age:"))
 4 job = input("job:")
 5 asg = '''
 6 Infomation of %s:
 7 Name:%s
 8 age:%d
 9 job:%s
10 ''' %(name,name,age,job)
11 print(asg)
12 -----------------------------------------------------
13 输入:
14 name:                 aa
15 age:21
16 job:              ff
17 #打印
18 Infomation of                  aa:
19 Name:                 aa
20 age:21
21 job:              ff

看到上面 添加空格后 输出就乱了
现在就需要把空格去掉了

  • 现在开始对字符串操作,可以用 .strip() 意思是去掉的意思
 1 # -*- coding:utf-8 -*-
 2 name = input("name:").strip()
 3 age = int(input("age:"))
 4 job = input("job:").strip()
 5 asg = '''
 6 Infomation of %s:
 7 Name:%s
 8 age:%d
 9 job:%s
10 ''' %(name,name,age,job)
11 print(asg)
12 --------------------------------------------------------------------------------
13 输入;
14 name:                        dddddd
15 age:21
16 job:         ff
17 打印:
18 Infomation of dddddd:
19 Name:dddddd
20 age:21
21 job:ff
  • strip() 可以指定去除内容,只能去除开头或者结尾的内容,中间的不会被去除,看下面的实验
 1 # -*- coding:utf-8 -*-
 2 name = input("name:").strip('aa')
 3 age = int(input("age:"))
 4 job = input("job:").strip('bb')
 5 asg = '''
 6 Infomation of %s:
 7 Name:%s
 8 age:%d
 9 job:%s
10 ''' %(name,name,age,job)
11 print(asg)
12 --------------------------------------------------------------------------------
13 输入:
14 name:aa
15 age:12
16 job:bbxx
17 打印:
18 Infomation of :
19 Name:
20 age:12
21 job:xx

name_list = ['aa','bb','cc']

  • 列表的好处是可以存储多个信息以前的变量存储量是有限的
  • 列表个变量里可以存储多个信息
  • 列表内的内容成为元素
  • 列表的索引值以0为起始值

取一个值
name_list[0]

  • 可以通过 dir(name_list)来查看可以操作的私有方法

只有:

append:追加

1 name_list.append("999")

count:统计

通过count可以统计出name_list中一共存在几个 aaa999,下面实验中统计出一共有3个 aaa999

 1 name_list.count("aaa999") 

extends:扩展

index:索引

索引值只能找到第一个

分别从 0为起始值 第一个aaa999排在第6个

 1 name_lsit.index("aaa9999") 

insert:插入

下面实验是讲在 第6个位置插入bbb9999

 1 name_lsit.insert(6,"bbb999") 

pop:删除一个

默认删除列表的最后一个

 1 name_list.pop() 

remove:删除指定一个

删除一个指定的值 aaa999

 1 name_list.remove("aaa9999") 

通过循环的方式remove删除

1 for i in range(name_list.count('9999'))
2     name_list.remove('9999')

reverse:反转

 1 name_list.reverse() 

sort:排序

按照阿斯克玛排序

 1 name_list.sort() 

------------------------------------------------------------------------------------

切片:

首先创建一个列表

 1 #创建一个列表
 2 name_list = [1,2,3,4,'a','b','c']
 3 #对这个name_lsit列表排序
 4 name_list.sort()
 5 #打印列表
 6 print(name_list)
 7 ------------------------------------------------------------------------------------
 8 #提示
 9 Traceback (most recent call last):
10   File "F:/python/day1/切片.py", line 3, in <module>
11     name_list.sort()
12 TypeError: unorderable types: str() < int()
13 ------------------------------------------------------------------------------------
14 #提示不能排序str()str是字符,int()是数字
15 #意思是说在列表里字符和数字在一起了所以不能排序

下面把 [1,2,3,4,'a','b','c'] 分成两部分变成 [1,2,3,4]----------['a','b','c']

1 name_list = [1,2,3,4,'a','b','c']
2 print(name_list[:4])
3 -----------------------------------------------------------------------------------
4 输出结果:
5 [1, 2, 3, 4]
 1 __author__ = 'Administrator'
 2 # -*- coding:utf-8 -*-
 3 name_list = [1,2,3,4,'a','b','c']
 4 #name_list.sort()
 5 #打印name_lsit从索引起始位置0开始到索引位置4结束
 6 print(name_list[0:4])
 7 #打印name_list 4开始到7结束
 8 print(name_list[4:7])
 9 #从中间开始切片,从索引其实卫视2开始到索引位置5结束
10 print(name_list[2:5])
11 #Python在切片这里看到name_list[0:4],name_list[4:7]索引位置两次被引用,可发现python切片的特点是顾首不顾尾
12 
13 
14 #跳着切片,从起始索引位置0开始到索引位置5结束,每隔两个切一次
15 print(name_list[0:5:2])
16 #------------------------------------------------------------------------------------
17 #输出
18 #从索引起始位置切分
19 [1, 2, 3, 4]
20 #从索引中间位置到索引结尾位置开始切分
21 ['a', 'b', 'c']
22 #从索引中间位置到索引中间位置结束
23 [3, 4, 'a']
24 #跳着切片
25 [1, 3, 'a']

下面倒着切片:

1 __author__ = 'Administrator'
2 name_list = [1,2,3,4,5,'a','b',]
3 #取倒数最后一位
4 print(name_list[-1])
5 #取倒数两位
6 print(name_list[-2:])
7 #取前三个
8 print(name_list[:3])

下面在看一下个排序

 1 __author__ = 'Administrator'
 2 name_list = [1,2,3,4,5,'a','b',]
 3 
 4 
 5 name_list.sort()
 6 print(name_list)
 7 ------------------------------------------------------------------------------------
 8 #输出
 9 Traceback (most recent call last):
10   File "E:/python/day1/下/day1/切片.py", line 5, in <module>
11     name_list.sort()
12 TypeError: unorderable types: str() < int()
13 #在3.0里 字符串和列表不能放在一起进行排序

 下面看一下 extend

extend 是列表合并

 1 方法一:
 2 __author__ = 'Administrator'
 3 name_list = [1,2,3,4,5,'a','b',]
 4 name_list2 = ['q','w','e','r',]
 5 
 6 Gg = name_list + name_list2
 7 print(Gg)
 8 ------------------------------------------------------------------------------------
 9 #输出:
10 [1, 2, 3, 4, 5, 'a', 'b', 'q', 'w', 'e', 'r']
11 
12 ------------------------------------------------------------------------------------
13 方法二:
14 __author__ = 'Administrator'
15 name_list = [1,2,3,4,5,'a','b',]
16 name_list2 = ['q','w','e','r',]
17 name_list.extend(name_list2)
18 print(name_list)
19 ------------------------------------------------------------------------------------
20 #输出:
21 [1, 2, 3, 4, 5, 'a', 'b', 'q', 'w', 'e', 'r']
原文地址:https://www.cnblogs.com/nb-blog/p/5111632.html