Python输入/输出语句

Python输出语句print函数

  • print()函数基本使用

    打印整形数据

    打印浮点型数据

    打印字符型数据

>>> print(12)
12
>>> print(12.5)
12.5
>>> print('B')
B
>>> print('WWW.baidu.com')
WWW.baidu.com
>>> x=12
>>> print(12)
12
>>> y=12.88
>>> print(y)
12.88
>>> z='www.bling.com'
>>> print(z)
www.bling.com
>>> print z
www.bling.com
>>> print x,y,z
12 12.88 www.bling.com
>>> print(z,y,z)
('www.bling.com', 12.88, 'www.bling.com')

  • print格式化输出

  print(format(val,fomat_modifier))

    var:值

    fomat_modifier:格式字

  print(12.34567,'m.nf')

    m:表示占位数量(m),m<有效位时,左对齐;m>有效位时,右对齐

    n:表示精度(n)

  print x,y  '逗号'连接输出

>>> print(format(12.345678,'6.2f'))
 12.35
>>> print(format(12.345678,'6.3f'))
12.346
>>> print(format(12.345678,'6.9f'))
12.345678000
>>> print(format(12.345678,'6.0f'))
    12
>>> print(format(12.345678,'6.3f'))
12.346
>>> print(format(12.345678,'6.2f'))
 12.35
>>> print(format(12.345678,'9.2f'))
    12.35
>>> print(format(12.345678,'3.2f'))
12.35
>>> print(format(12.345678,'5.2f'))
12.35

  print()格式化输出format

    print(format(val,fomat_modifier))

    print(format(0.3456,'.2%'))

>>> print(format(0.3456,'.2%'))
34.56%
>>> print(format(0.3456,'.3%'))
34.560%
>>> print(format(0.3456,'.6%'))
34.560000%
>>> print(format(0.3456,'6.3%'))
34.560%
>>> print(format(0.3456,'6.1%'))
 34.6%
  • Python输入函数

  re = raw_input(prompt)

  prompt:提示字符,从键盘读取数据

  re:返回值,返回字符串 

  类型转换:int()(转为整形)、float()(浮点型)

>>> re = raw_input()
www.bling.com
>>> print(re)
www.bling.com
>>> type(re)
<type 'str'>
>>> age = raw_input('your age :')
your age :22
>>> age = age +1

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    age = age +1
TypeError: cannot concatenate 'str' and 'int' objects
>>> age = int(age)
>>> age
22
>>> age = age +1
>>> age
23
>>> type(age)
<type 'int'>
>>> age = int(raw_input('PLZ input your age :'))
PLZ input your age :23
>>> type(age)
<type 'int'>
>>> f1 = float('12.35')
>>> type(f1)
<type 'float'>
>>> weight = float(raw_input('Plz input your weight :'))
Plz input your weight :70.01
>>> print(weight)
70.01
>>> type(weight)
<type 'float'>
>>> 
原文地址:https://www.cnblogs.com/yangml/p/3849971.html