python - 字符串的格式化输出



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

'''
@project: jiaxy
@author: Jimmy
@file: study_2_str.py
@ide: PyCharm Community Edition
@time: 2018-11-01 15:12
@blog: https://www.cnblogs.com/gotesting/

'''


# 字符串
s = '' #空字符串
# 1:字符串拼接
# 1.1:字符串与字符串的拼接用 + 连接

s_1 = 'hello'
s_2 = 'world'
s_3 = 5201314

new_s_1 = s_1 + s_2
print('拼接后的字符串:',new_s_1)

# 1.2:如果是字符串和数字拼接,可以将数字强制转换成字符串 str()

new_s_2 = s_1 + s_2 + str(s_3)
print('字符串与数字拼接:',new_s_2)


# 2:字符串的格式化输出
# % 占位符/占坑符 : %s 字符串; %d 整数 ;%f 浮点数
name = 'Jimmy'
course = 'Python'
class_id = 12
age = 18
salary = 1000000


print('''======== Jimmy's info =======
姓名:%s
课程:%s
班级:%d
年龄:%d
薪酬:%d
========= Wishing ===========
''' %(name,course,class_id,age,salary)

)

# 格式化输出方式二 format {}

# {}不指定数据 按顺序赋值
print('''======== Jimmy's info =======
姓名:{}
课程:{}
班级:{}
年龄:{}
薪酬:{}
========= Wishing ===========
''' .format(name,course,class_id,age,salary)

)

# {}里面指定数据,按照指定数据赋值
print('''======== Jimmy's info =======
姓名:{4}
课程:{3}
班级:{2}
年龄:{1}
薪酬:{0}
========= Wishing ===========
''' .format(name,course,class_id,age,salary)

)

print('我是{},我今年{}岁'.format('Jimmy',18))



 


posted @ 2018-11-01 15:47  JiaxyGogogo  阅读(201)  评论(0编辑  收藏  举报