美文网首页
Python几种格式化输出

Python几种格式化输出

作者: 南少cc | 来源:发表于2017-04-14 15:37 被阅读0次

Python格式化输出

name = input('name:')
age = input('age:')
userInfo = '''
---------------- welcome ''' + name + '''-----------
name:'''+name+'''
age:''' + age
userInfo1 = '''
---------------- welcome %s -----------
name:%s
age:%s
''' % (name,name,age)
userInfo2 = '''
---------------- welcome {_userName} -----------
name:{_userName}
age:{_userAge}
'''.format(_userName = name,
           _userAge = age)
userInfo3 = '''
---------------- welcome {0} -----------
name:{0}
age:{1}
'''.format(name, age)

print(userInfo)
print(userInfo1)
print(userInfo2)
print(userInfo3)

打印如下:四种方式效果一样

---------------- welcome xubojoy -----------
name:xubojoy
age:20

温度转换

fahrenheit = 0
while fahrenheit <= 250:
    celsius = (fahrenheit - 32) / 1.8
    print('%5d %7.2f' % (fahrenheit,celsius))  #等价于print('{:5d} {:7.2f}'.format(fahrenheit, celsius))  #%5d 或者 {:5d} 默认保留5个位置的整数   %7.2f或者{:7.2f} 默认占7个单位的保留两位小数的浮点型
    fahrenheit = fahrenheit + 25

相关文章

  • Python几种格式化输出

    Python格式化输出 print(userInfo)print(userInfo1)print(userInfo...

  • 实战

    python的格式化输出 #python格式化输出 ##%对于未知变量类型,用这样就不太方便了 name='lis...

  • format方法

    在python编程规范中,提出字符串格式化输出尽量用format函数而不是%。 主要包括以下几种: 1、直接用中括...

  • 入门输入输出篇

    python 的输入和输出 输出 print('hello') 格式化输出: 命令行: >>> 'Hello, %...

  • Python2与Python3中print用法总结

    Python2中的print用法 在Python2 中 print 是一种输出语句 1.格式化输出整数 2.格式化...

  • Python 中的常见 格式化符号

    Python 认识格式化输出 中的 格式化符号 在前面的文章里我们早早就接触过Python中的输出的函数prinn...

  • python—输入与输出

    Python的格式化输出 使用字符串格式化的形式来优化Python的输出,使用%作为占位符。%后面跟的是变量的类型...

  • Python使用format与f-string数字格式化

    Python使用format与f-string数字格式化 输出:

  • python笔记

    Python format格式化输出 浅谈 Python 的 with 语句 Python中迭代原理生成器和迭代原...

  • python-print函数的使用

    1.格式化输出 看看《Python基础编程》中对格式化输出的总结: %字符:标记转换说明符的开始 转换标志:-表示...

网友评论

      本文标题:Python几种格式化输出

      本文链接:https://www.haomeiwen.com/subject/cngwattx.html