Python中字符串的学习
Python中字符串的学习
一、字符串的格式化输出
- % 占位符
- %s 字符串
- %d integer
- %x 十六进制 integer
- %f float
- 指定长度
- %5d 右对齐,不足左边补空格
- %-5d - 代表左对齐,不足右边默认补空格
- %05d 右对齐,不足左边补0
-浮点数:- %f 默认是输出6位有效数据, 会进行四舍五入
- %.2f 指定小数点位数的输出 保留小数点后2位
- '%4.8f' 4代表整个浮点数的长度,包括小数,只有当
字符串的长度大于4位才起作用.不足4位空格补足,可以用%04.8使用0补足空格
二、字符串的format方法
- 顺序填坑:{} 占位符
- 下标填坑
- 变量填坑
- 对齐
- {:5} 指定输出长度=5
- 字符串 {:5}--左对齐
- 数值 {:5}--右对齐
- 使用 > < 可以避免字符串/数值对齐方法不一致
- > 右对齐
- < 左对齐
- 中间对齐 ^
- 不足的长度用*表示
三、格式化 f''
- python3.6 后的版本支持。
- f'名字是:{name},年龄是:{age}'
四、测试代码:
name = 'xiaolee'
age = 45
height = 1.7234782342
intro1 = 'My name is %s, I\'m %d years old, and my height is %f Metres.'
intro2 = 'My name is %s, I\'m %10d years old, and my height is %.2f Metres.'
intro3 = 'My name is %s, I\'m %-10d years old, and my height is %4.2f Metres.'
intro4 = 'My name is %s, I\'m %010d years old, and my height is %08.2f Metres.'
intro5 = 'My name is %s, I\'m %x years old, and my height is %08.2f Metres.'
intro6 = 'My name is {}, I\'m {} years old, and my height is {} Metres.'
intro7 = 'My name is {2}, I\'m {1} years old, and my {0} is %08.2f Metres.'
intro8 = 'My name is {name0}, I\'m {age0} years old, and my height is {height0} Metres.'
intro9 = 'My name is {0:*<15}, I\'m {1:*>11} years old, and my height is {2:*^20} Metres.'
introA = 'My name is {0:<15}, I\'m {1:>11} years old, and my height is {2:^20} Metres.'
introB = 'My name is {0:#<15}, I\'m {1:$>11} years old, and my height is {2:8^20} Metres.'
introC = f'My name is {name}, I\'m {age} years old, and my height is {height} Metres.'
print(intro1 % (name, age, height))
print(intro2 % (name, age, height))
print(intro3 % (name, age, height))
print(intro4 % (name, age, height))
print(intro5 % (name, age, height))
print(intro6.format(name, age, height))
print(intro7.format(height, age, name))
print(intro8.format(age0=age, name0=name, height0=height))
print(intro9.format(name, age, height))
print(introA.format(name, age, height))
print(introB.format(name, age, height))
print(introC)
程序执行结果:
本文主要参照这篇博文,测试程序重新写。
Python 字符串格式化输出的3种方式
Python中字符串的学习的更多相关文章
- python中字符串的几种表达方式(用什么方式表示字符串)
说明: 今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 ...
- python中字符串的四种表达方式
今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 > ...
- 【Python从入门到精通】(九)Python中字符串的各种骚操作你已经烂熟于心了么?
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要. 干货满满,建议收藏,欢迎大 ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
- Python中字符串与字节之间相互转换
Python中字符串与字节之间相互转换 a = b"Hello, world!" # bytes object b = "Hello, world!" # ...
- 超详细!盘点Python中字符串的常用操作
在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...
- Python中字符串有哪些常用操作?纯干货超详细
随机推荐
- VS Code 1.42 发布!2020 年首个大更新
近日(北京时间 2020 年 2 月 7 日),微软发布了 Visual Studio Code 1.42 版本,这也是 2020 年 VS Code 首次大更新.让我们来看看有哪些主要的更新. 支持 ...
- 脚本实现统计osd内的pg数量
脚本代码如下: ceph pg dump | awk ' /pg_stat/ { col=; while($col!=“up”) {col++}; col++ } /[-9a-f]+.[-9a-f]+ ...
- 简明 homebrew
介绍 包管理工具几乎已经成为现代操作系统或者开发平台不可或缺的工具软件,无论做开发,或是管理服务器,都免不了用到一些第三方依赖包.包管理工具的基本功能就是提供一个集中的平台,可以在这里找到大部分流行的 ...
- 使用卷影拷贝提取ntds.dit
一.简介 通常情况下,即使拥有管理员权限,也无法读取域控制器中的C:\Windows\NTDS\ntds.dit文件.使用windows本地卷影拷贝服务,就可以获得该文件的副本. 在活动目录中,所有的 ...
- Codeforces_842
A.枚举一个区间,判断是否有数符合. #include<bits/stdc++.h> using namespace std; long long l,r,x,y,k; int main( ...
- 基于 HTML5 Canvas 的病毒模拟视觉试验台
前言 2020 年 1 月 12 日,世界卫生组织以武汉病毒肺炎病例命名了一种病毒,2019新型冠状病毒 ( 2019-nCoV ) .随着春运的到来,该病毒迅速的蔓延开来,大家都唯恐避之不及.病毒的 ...
- LVM 逻辑卷 (logica volume manager)
逻辑卷轴管理员 (Logical Volume Manager) 想像一个情况,你在当初规划主机的时候将 /home 只给他 50G ,等到使用者众多之后导致这个 filesystem 不够大, 此时 ...
- [Python]获取字典所有值
方法一:Key Value 直接获取 databases = {1: 'Student', 2: 'School'} for k,v in databases.items(): print(k,v) ...
- 写给Unity开发者的iOS内存调试指南
0x00 前言 工作的过程中,常常会发现有小伙伴对Unity的Profiler提供的内存数据与某些原生平台Profiler工具,例如iOS系统和Xcode,所提供的内存数据有差异而感到好奇.而且大家对 ...
- 解决打开的小窗口回调参数的问题,layui
问题: 给用户新增优惠券,单个用户,单个优惠券,新增的话,用输入窗点击事件,点击后弹出一个子窗口列表页,选择数据后,点击确定,信息传回父窗口,主要是解决传值的问题. 解决思路: ...