【Py】Python基础——基础数据类型

基本数据类型

  • 整数int

  • 浮点数float

  • 字符串str

  • 布尔值bool

    True False
    

可以用type()来判断变量类型。

字符串与其他的转换

  • 字符串转整数

    int('8')
    
  • 整数转字符串

    str(8)
    

整数与浮点

print(int(1.8)) #抹掉小数部分,保留整数部分
print(8/10) #0.8
print(int('10',16) #16进制

字符串

  • 单引号双引号一样,字符串中有单引号时可以用双引号。

  • 可切片[1:2]

  • 可相加可相乘

转义

\ \n

格式化输出

print "My name is %s and weight is %d kg!" % ('Zara', 21)

字符串方法

s='ss  ss '
s.find('ss')# 返回开始的索引或-1
list1=s.split(s2)# 以字符串s2分割s,返回list
s=s.replace(s3,s4)# 把s中的s3字符串换成s4 
s = s.strip()# 去掉左右的空格、换行符
posted @ 2020-06-16 20:01  Ryan_W  阅读(135)  评论(0编辑  收藏  举报