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