博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python - 字符串的格式化输出
阅读量:6983 次
发布时间:2019-06-27

本文共 1200 字,大约阅读时间需要 4 分钟。

# -*- 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))

 

 

转载于:https://www.cnblogs.com/gotesting/p/9889670.html

你可能感兴趣的文章
[SCOI2009]windy数
查看>>
Struts2--Action属性接收参数
查看>>
2012年科技新闻背后的大数字
查看>>
报价单内,同一物料只允许一条行价格记录
查看>>
leetcode 283. Move Zeroes
查看>>
自己的php函数库
查看>>
HDU Problem 1599 find the mincost route 【Floyd求最小环】
查看>>
HDU2017多校联合 contest 1
查看>>
基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理(转载自:http://www.cnblogs.com/wuhuacong/p/4175266.html)...
查看>>
range
查看>>
[Noi2002]Savage 题解
查看>>
特征选择, 经典三刀(转)
查看>>
【Python3爬虫】自动查询天气并实现语音播报
查看>>
新的公司
查看>>
【JavaScript】02.基础语法学习
查看>>
自行学习XAML控件后的简单想法(作业一)
查看>>
python3 集合中的常用方法
查看>>
ECSHOP生成缩略图模糊
查看>>
LayaAir疑难杂症之四:laya引擎自动断点到bundle.js文件中且无报错,但程序不再执行...
查看>>
元组操作
查看>>