从零开始学Python(第五天)


在这里插入图片描述

第五天

5.1 字符串对象

​ 1、什么是字符串
​ ‘’、" " 、 " ““字符串””"、 ‘’‘字符串 ‘’’
​ 2、字符串常见的方法
​ 字符串可以被迭代,也可以通过下标访问
​ 注意:字符串是不可变类型

python中字符串的所有方法,并不会改动字符串本身的值!!!
而是返回值发生了变化

5.1.1 常用方法

	|-- capitalize()				# 首字母大写
	|-- center(width[,fillchar])	 # 居中对象,第二个参数可选,表示要填充的符号,默认为空格
	|-- count(char)					# 统计字符数量
	|-- endswith(char)				# 以xxx结束
	|-- startswith(char)			# 以x开始
	|-- find(char)					# 用来检索某个字符或者字符串在该字符串中的第一次出现索引位置,如果找不到呢?则返回-1
	|-- rfind(char)					# 从最后一个向前查,但是下标并没有发生变化
	|-- index(char)					# 用来检索某个字符或者字符串在该字符串中的第一次出现索引位置,如果找不到呢?则抛出异常
	|-- rindex(char)		# 查找最后一个
	|-- format()			# 格式化字符串,python提出了,建议大家使用
	|-- join(list)			# 按照特定的规则拼接字符串
	|-- split(char)			# 按照特定的字符串分割字符串,结果是列表
	|-- rsplit()			# 从右
	|-- upper()				# 转大写
	|-- lower()				# 转小写
	|-- strip()				# 清除两侧的空格
	|-- lstrip()			# 清除左侧空格
	|-- rstrip()			# 清除右侧空格
	|-- title()				# 将字符串格式化为符合标题的格式
	|-- replace(old_str, new_str)	# 替换字符串
	|-- encode()					# 将字符串转换为字节

​ 注意:字节对象中有一个decode方法,可以将字节转换为对应编码的字符串

is开头的是用于判断的

​ isalnum()
​ isalpha()
​ isdigit()

5.1.2 实际演示

capitalize()
>>> s = "hello,my name is eichi"
>>> s
'hello,my name is eichi'
>>> s.capitalize()
'Hello,my name is eichi'

center()
>>> s.center(50)
'              hello,my name is eichi              '
>>> s.center(50,"*")
'**************hello,my name is eichi**************'

count()
>>> s.count("l")
2
>>> s.count("i")
3

endswith()、startswith()
>>> s.endswith("xxx")
False
>>> s.endswith("i")
True
>>> s.endswith("eichi")
True
>>> s.startswith("hello")
True
>>> s.startswith("helo")
False

find()、index() 二者查询时作用一致,区别在于find()找不到时,返回-1
>>> ss = "12345"
>>> ss.find("1")
0
>>> ss.find("5")
4
>>> ss.find("6")
-1
>>> ss.index("1")
0
>>> ss.index("3")
2
>>> ss
'12345'
>>> ss.index("1")
0
>>> ss.index("5")
4
>>> ss.index("6")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
    
 rfind()、rindex() 
>>> s = "hi,cool,hello"
>>> s.find("l")
6
>>> s.rfind("l")
11
>>> s.rfind("p")
-1
>>> s.rindex("p")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
    
 format()
>>> num1 = 10
>>> num2 = 20
>>> print("{}+{}={}".format(num1,num2,num1+num2))
10+20=30

split()
>>> ss = "http://www.baidu.com"
>>> ss
'http://www.baidu.com'
>>> ss.split(".")
['http://www', 'baidu', 'com']
>>> ss.split("w")
['http://', '', '', '.baidu.com']

join()
>>> ls = ["1","2","3","4"]
>>> ls
['1', '2', '3', '4']
>>> " ".join(ls)
'1 2 3 4'

upper()、lower()
>>> ss = "hello,python"
>>> s = ss.upper()
>>> s
'HELLO,PYTHON'
>>> s.lower()
'hello,python'

encode()、decode()
>>> ss
'hello,python'
>>> ss.encode("utf-8")
b'hello,python'
>>> sb = ss.encode("utf-8")
>>> sb
b'hello,python'
>>> sb.decode("utf-8")
'hello,python'

5.2 切片

5.2.1 概述

切片是python提供给开发者用来分割、切割字符串或者其他有序可迭代对象的一种手段

字符串[index]		# 访问字符串的某个字符
字符串[start:]		# 从start下标位置开始切割字符串,到末尾
字符串[start:end]	# 从start下标位置开始切割字符串,切去end位置,不包含end 前闭后开区间[)
字符串[start:end:step]	# step表示步长,默认是1,注意:如果step为负数,表示从右向左切取

python是从在负索引的。最后一个位是-1,倒数第二个是-2,以此类推
​注意:切片操作,如果下标不存在,并不会报错,切片是用来切割字符串的,不会改变字符串的值

索引存在正负索引之分

正索引:从0到下标减一

负索引:从-1到-长度下标

字符串、元组、列表可以使用索引,集合不能使用索引(集合是无序的)

5.2.2 实际展示

>>> s = "0123456789"
>>> s[0]
'0'
>>> s[9]
'9'
>>> s[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[1:]
'123456789'
>>> s[1:8]
'1234567'
>>> s[4:]
'456789'
>>> s[1:7:2]
'135'

字符串中所有方法,并不能修改字符串本身的值

字符串是一个常量,python中没有切割字符串的方法

面试题:

一行代码反转字符串

>>> s = "1234567"
>>> s[::-1]
'7654321'

>>> ls = list(s)
>>> ls
['1', '2', '3', '4', '5', '6', '7']
>>> ls.reverse()
>>> ls
['7', '6', '5', '4', '3', '2', '1']

5.3 函数

5.3.1 函数概述

​ 1、什么是函数
​ 具有特殊功能的一段代码的封装
​ C语言是一门面向过程(功能、函数)的语言
​ 具有名称的一段具体功能代码的统称
​ 2、为什么学习函数
​ 代码封装、提高代码的复用性、解耦合的作用

5.3.2 函数的定义

使用关键字 def # define function

	def 函数名称([参数列表]):
		# 函数体

		# [return 返回值]

​ 调用函数的帮助文档
​ print(print_msg.doc)
​ print(help(print_msg))

5.3.3 函数的调用

​ 函数名称([参数列数])

函数演示:

import math

def get_area(i,j,k):
	p = 0.5 * (i+j+k)
	s = math.sqrt((p*(p-i)*(p-j)*(p-k)))
	return s


a,b,c = eval(input("请输入三角形的三个边的长度"))
x = get_area(a,b,c)

print('三边为{}、{}、{}的三角形的面积是:{:.2f}'.format(a,b,c,x))

#执行代码
D:\网络安全\Python\py_code>python Class3_三角形面积(函数).py
请输入三角形的三个边的长度3,4,5
三边为345的三角形的面积是:6.00

今天的内容到这里就结束了~ 需要做题巩固练习的,请保持关注,持续更新中ing

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章