十格式化输出

Posted linjiahua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十格式化输出相关的知识,希望对你有一定的参考价值。

格式化输出

age = input('age:>>>')

print('My age is'+age)
age:>>>19
My age is19
age = input('age:>>>')

print('My age is '+age)
age:>>>19
My age is 19
age = input('age:>>>')

print('My age is',age)
age:>>>19
My age is  19
name = input('name:>>>')
age = input('age:>>>')
height= input('height:>>>')

print('My name is %s, My age is %s, My height is %s'
name:>>>qiangge
age:>>>16
height:>>>175
My name is qiangge , My age is 16 , My height is 175

占位符

name = input('name:>>>')
age = input('age:>>>')
age = int(age)
height= input('height:>>>')

print('My name is %s, My age is %s, My height is %s'%(name,age,height))
name:>>>skdfj
age:>>>12
height:>>>12
My name is skdfj, My age is 12, My height is 12
print('My name is %s, My age is %s, My height is %s')
My name is %s, My age is %s, My height is %s
name = input('name:>>>')
name = int(name)



age = input('age:>>>')
age_int = int(age)

age = int(input('age:>>>'))




height= int(input('height:>>>'))


print('My name is %d, My age is %d, My height is %d'%(name,age,height))
name:>>>1
age:>>>1
height:>>>1
My name is 1, My age is 1, My height is 1
print(name)

format格式化(了解)

name = input('name:>>>')
age = input('age:>>>')
height= input('height:>>>')


# print('My name is %d, My age is %d, My height is %d'%(name,age,height))
print('My name is {}, My age is {}, My height is {}'.format(name,age,height))
name:>>>nick
age:>>>234
height:>>>234
My name is nick, My age is 234, My height is 234
print('My name is {0}, My age is {1}, My height is {2}'.format(name,age,height))
My name is nick, My age is 234, My height is 234
print('My name is {0}, My age is {0}, My height is {0}'.format(name,age,height))
My name is nick, My age is nick, My height is nick
print('My name is {age}, My age is {name}, My height is {height}'.format(name=name,age=age,height=height))
My name is 234, My age is nick, My height is 234

f-string格式化

name = input('name:>>>')
age = input('age:>>>')
height= input('height:>>>')


# print('My name is %d, My age is %d, My height is %d'%(name,age,height))
# print('My name is {}, My age is {}, My height is {}'.format(name,age,height))
print(f'My name is {age}, My age is {name}, My height is {height}')
name:>>>zhaogang
age:>>>18
height:>>>160
My name is 18, My age is zhaogang, My height is 160
print(f'My name is {name}, My age is {int(age)+1}, My height is {height*10}')
My name is zhaogang, My age is 19, My height is 160160160160160160160160160160
print(f'My name is {"fanping"}, My age is {int(age)+1:.5f}, My height is {height*10}')
My name is fanping, My age is 19.00000, My height is 160160160160160160160160160160

:.2f是保留两位小数

以上是关于十格式化输出的主要内容,如果未能解决你的问题,请参考以下文章

1006. 换个格式输出整数 (15)

SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段

1006

uvec2片段着色器输出的哪个组合

PAT1006

Golang PrintfSprintf Fprintf 格式化