python的数据类型

一、整形

1、

  eg:  1,2,3,4......

2、python2.6中 :raw_input 和 input的区别:

x=raw_input("please input a str:")
print("%s is %s" % (x,type(x) ))
y=input("please input a number:")
print("%s is %s" % (y,type(y)))

分别输入100 得出结果:

  please input a str:100
  100 is <type 'str'>
  please input a number:100
  100 is <type 'int'>

3、python3中只有raw_input ,而且存的类型是str

二、浮点型

1、

  eg: 1.1   ,  1.0  .....

2、

round(float)函数:默认四舍五入

eg:  保留2位小数

>>> a=1.278
>>> round(a,2)
1.28

注意:应为是保留2位小数,如果把5进位了变成1.28-->1.3 ,出现这种情况python则不会把5进位,所以答案是1.27

>>> a=1.275
>>> round(a,2)
1.27

三、布尔型

1、

  eg: True ,False , 1 ,0

  在程序中经常和if 语句等配合使用

四、字符串

常用操作:

find() #找到返回序号,找不到返回-1

>>> a="abc"
>>> a.find("b")

1

>>> a.find("a")
0

>>> a.find("x")
-1

replace(s,d)    # 替换,注意不是原值替换,只是输出替换后的值,如果要用则使用变量保存起来

>>>a="abc"

>>> a.replace("b","a")
'aac'
>>> a
'abc'

split()     # 以什么做分割该字符串

>>> a="abc"
>>> a.split("b")
['a', 'c']

join()   #拼接

>>> print('**'.join('fxh'))
f**x**h

strip()  # 去除空格

info=" My name is fxh , I'am a man !!! "
info1=info.strip()
print(info)
print(info1) #去除了左右两边的空格

format() 

name="fxnxuanhui"
age="24"
print("hi "+name + " my age is:" +age)
print("hi %s my age is: %s" % (name,age))
print("hi {0} my age is: {1}".format(name,age))
print("hi {n} my age is: {a}".format(a=age,n=name))

 输出结果多是: hi fxnxuanhui my age is: 24



原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/7702192.html