列表/元组/集合/字典/字符串

2023-08-12  本文已影响0人  花开有声是我

数字/字符串/列表/元组/集合/字典: number/String/list/tuple/dictionary/Dictionary
方法详见:https://www.runoob.com/python/python-lists.html
https://blog.csdn.net/u013355826/article/details/78761742

python的数据类型:

一、数字

数字(Number)支持三种数值类型:整形(int)、浮点型(float)、复数(complex)
int(x)将x转换为一个整数
float(x)将x转换到一个浮点型
complex(x)将x转换到一个复数,实数部分为x,虚数部分为0(x+0j)
python运算:/:返回的是浮点数, //:向下取整数, **:幂运算

二、字符串

字符串是单引号或双引号中的数据,例如"123abc"、'a'
python访问字符串中的值

str1 = "helloworld"
print(var1[0]) #  h
print(var1[1:3]) # el

字符串切片str[x:y],str[y]不包括在内

str = "happy"
str[1:3] # "ap" 获取从偏移为1到偏移为3的字符串,不包括偏移为3的字符
str[1:] # "appy" 获取从偏移为1到最后一个字符
str[:3] # "hap" 获取从偏移为0到3到字符串,不包括偏移为3的字符串
str[:-1] # "happ" 获取从偏移为0的字符一直到最后一个字符串,不包括最后一个字符
str[:] # "string" 获取从开始到结尾的所有元素、
str[::-1] # "yppah" 反转输出

“+”字符串拼接
字符串方法
replace() str.replace("old", "new", 1)
find()

上一篇下一篇

猜你喜欢

热点阅读