python字符串格式化

#! /usr/bin/env python

# -*- coding:utf-8 -*-

#字符串格式化

#%s接收任何类型的值

s ='i am %s and is %s' %('time' , 'python')

print(s)

#

# #%d接收数值

s ='i am %s and is %d' %('time' , 22)

print(s)

# #%f接收浮点数,并且可以指定获取几位小数

s ='i am %s and is %.2f' %('time' , 2.22432)

print(s)

#以字典形式来传值

s ='i am %(name)s and is %(age)d' %{"name":"tiome" , "age":22}

print(s)

#打印百分比

tpl ="percent %.2f" %99.976234444444444444

print(tpl)

print('root','x','0','0',sep=':')

你可能感兴趣的:(python字符串格式化)