python-输出格式化

#格式化输出方式1-多行字符串拼接  +,!!!!不推荐使用,占用内存太多
name = raw_input("Your name : ")
age = raw_input("Your age : ",)
info = '''
The name is: '''+name+'''
The age is:'''+age+'''
'''
print info


#格式化输出方式2-变量替换%s  %s和%d,%s是用来给字符串占位置,%d是给数字占位置
name = raw_input("Your name : ")
age = raw_input("Your age : ")
info = '''
----Info of %s----
The name is: %s
The age is:%d
''' %(name,name,int(age))  #此处要做数据类型转换int(age),否则报错
try:    
    print info
except Exception,e:  
    print Exception,":",e            
    
#格式化输出方式3-_字符1 = 变量   format方法:
name = raw_input("Your name : ")
age = raw_input("Your age : ")
info = '''
----Info of {_xingming}----
The name is: {_xingming}
The age is:  {_nianling}
''' .format(_xingming = name,
            _nianling = age)  
print info

你可能感兴趣的:(python-输出格式化)