python3基础:格式化输出

直接简单的输出#简单输出一个字符串
>>>print(‘hello python apple‘)
hello python apple

#简单输出多个字符串
>>>print(‘hello‘,‘python‘, ‘apple‘)

hello python apple
#简单输出拼接字符串>>>print(‘hello‘+ ‘\tpython‘+ ‘\tapple‘)
hello  python  apple

格式化输出 --为了让输出更加美观主要是   1.%用法     2.format用法1. %()
%d 数字%s 字符串%f 浮点数
print(‘‘‘===我的个人资料===
name:%s
sex:%s
age:%d
height:%f
‘‘‘%(‘我的家在东北‘,"boy",15,70.66))
#总结  %s放任何数据   %d%f 放数字

输出结果:

===我的个人资料===
name:我的家在东北
sex:boy
age:15
height:70.660000

2.另一种方式 {}字符串.format()

print(‘‘‘===我的个人资料===
name:{0}
sex:{1}
age:{2}
height:{3}
‘‘‘.format(‘我的家在东北‘,"boy",15,70.66))

输出结果:

===我的个人资料===
name:我的家在东北
sex:boy
age:15
height:70.66
注意的点 1.{}要比()输入的少; 2.{}索引里面的值从0开始; 3.{}里面的索引都要同时给或者同时不给
练习一下
# name、age sex address hobby salary work_year
print(‘‘‘===ta的个人信息===
name:%s
sex:%s
age:%d
address:%s
hobby:%s
salary:%0.2f
work_year:%d
‘‘‘%(‘ta‘,"boy",15,‘我的家‘,‘play basketball‘,7000.55,3))

#第二种方法
print(‘‘‘===ta的个人信息===
name:{}
sex:{}
age:{}
address:{}
hobby:{}
salary:{}
work_year:{}
‘‘‘.format(‘ta‘,"boy",15,‘我的家‘,‘play basketball‘,7000.55,3))

输出结果

===ta的个人信息===
name:ta
sex:boy
age:15
address:我的家
hobby:play basketball
salary:7000.55
work_year:3

===ta的个人信息===
name:ta
sex:boy
age:15
address:我的家
hobby:play basketball
salary:7000.55
work_year:3


原文地址:https://www.cnblogs.com/bugbreak/p/12425056.html

时间: 2024-08-29 18:40:13

python3基础:格式化输出的相关文章

Python 基础 格式化输出

Python 基础 格式化输出 现在需要我们录入我们身边好友的信息,格式如下: ------------ info of Alex Li ---------- Name : Alex Li Age : 22 job : Teacher Hobbie: girl ------------- end ---------------- 我们现在能想到的办法就是用一下方法: name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入职业:'

python 基础格式化输出

a='this is a' b=a.replace('a','b') 字符串替换 c='this is %s %s' %('my','apple') d='this is {} {} ' .format('my','apple') e='this is {1} {0} ' .format('apple','my') 格式化输出,输出顺序 f='this is {whose} {fruit} ' .format(fruit='apple',whose='my') g='this is %(whos

python基础---格式化输出和运算符

格式化输出与运算符 1.格式化输出 name='egon'print('myname is '+name)print('myname is',name) 如果需要传多个变量,这种方法就不合适了 使用%s.%d来接受变量 %s:既可以接受数字,也可以接受字符串 %d:只能接受数字 name='egon'age='18'print('my name is: %s my age is %s' %(name,age)) 练习: 打印某用户的姓名.年龄.工作.爱好等信息 users=[{'username

Python3基础 print 输出hello world

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 >>> print("hello world") hello world ------------------------------------------博文的精髓,在技术部分,更在镇场一诗.Python是优秀的语言,值得努力学习.我是跟着小甲鱼视频教程学习的,

Python3基础 format 输出花括号{}

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: str1="{{0}}".format("找不到要填的坑") print(str1) result: ============= RESTART: C:/Users/Administrator/Desktop/mytest4.py ========

Python3基础 print 输出helloworld

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: >>> print("hello world") result: 1 hello world ------------------------------------------博文的精髓,在技术部分,更在镇场一诗.Python版本3.5,系统 Wi

Python3基础 print , 输出多个数据

? ???????Python : 3.7.0 ?????????OS : Ubuntu 18.04.1 LTS ????????IDE : PyCharm 2018.2.4 ??????Conda : 4.5.11 ???typesetting : Markdown ? code coder@Ubuntu:~$ source activate py37 (py37) coder@Ubuntu:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:1

python基础_格式化输出(%用法和format用法)

%用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 >>> print('%o' % 20) 24 >>> print('%d' % 20) 20 >>> print('%x' % 20) 14 2.浮点数输出 2.1 格式化输出 %f ——保留小数点后面六位有效数字 %.3f,保留3位小数位%e ——保留小数点后面六位有效数字,指数形式输出 %.3e,保留3位小数位,使用科学计数法%g ——在保证

Win10下安装Python3及Python2、数据类型、格式化输出、运算符

Win10下安装Python3及Python2 下载的官网地址: https://www.python.org/downloads/windows/ 安装Python3: 安装完成之后,在开始处输入 cmd ,测试Python是否安装成功. 输入: python -V ----> pip -v ----> pip 安装Python2: 安装完成之后,打开之前打开的命令提示符页面,测试Python是否安装成功. 输入: python -V ----> pip -v Python3.x与Py