《Python编程 从入门到实践》-基础知识补充

102 阅读1分钟

1.请在终端输出(包括最外层双引号):

"The language 'Python' is named afterMonty Python, not the snake."

代码:print ('"'"The language 'Python'is named after Monty Python, not the snake."'"')

或者:print ('"'+"Thelanguage 'Python' is named after Monty Python, not the snake."+'"')

注意:输出双引号用单引号括起来输出   +为连接符

\

2.字符串“ don not give up. Never give up until the flight is over!”尾部感叹号

代码:msg=" don not give up. Never give up until the flight is over!"    

          print (msg[:-1])

或者:print(msg.rstrip("!"))  

注意:print (msg[:-1]) 这句话没有删除最后一个字符的意思,是截取(术语叫做slice,切片),是指将字符串的开头起截取到字符串末尾倒数第二个字符,并输出,所以感叹号没有了

          这里,msg的值不仅是一个字符串,它是一个 String 对象,包含 rstrip() 的去尾方法,括号里是可以写上其他参数的,可以删除任意符号

3.插入三个字符串(append,insert,extend)

A.extend(["wang","lin","jiao"])

临时逆序排序(sort/sorted可以写多个参数)

print(sorted(A,reverse=True))

插入字符串(insert)

A.insert(0,'5')

删除值(pop(2),2为索引值 remove移除字符)

A.pop(2)

枚举(enumerate(p1,p2))参数p1指列表,p2代表设置索引值起始位置,默认为0

for index,item in enumerate(A,1):
	print(str(index)+'\t'+item)


\

\