Python中字符串的学习

一、字符串的格式化输出

  1. % 占位符

    • %s 字符串
    • %d integer
    • %x 十六进制 integer
    • %f float
  2. 指定长度
    • %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}'

四、测试代码:

  1. name = 'xiaolee'
  2. age = 45
  3. height = 1.7234782342
  4. intro1 = 'My name is %s, I\'m %d years old, and my height is %f Metres.'
  5. intro2 = 'My name is %s, I\'m %10d years old, and my height is %.2f Metres.'
  6. intro3 = 'My name is %s, I\'m %-10d years old, and my height is %4.2f Metres.'
  7. intro4 = 'My name is %s, I\'m %010d years old, and my height is %08.2f Metres.'
  8. intro5 = 'My name is %s, I\'m %x years old, and my height is %08.2f Metres.'
  9. intro6 = 'My name is {}, I\'m {} years old, and my height is {} Metres.'
  10. intro7 = 'My name is {2}, I\'m {1} years old, and my {0} is %08.2f Metres.'
  11. intro8 = 'My name is {name0}, I\'m {age0} years old, and my height is {height0} Metres.'
  12. intro9 = 'My name is {0:*<15}, I\'m {1:*>11} years old, and my height is {2:*^20} Metres.'
  13. introA = 'My name is {0:<15}, I\'m {1:>11} years old, and my height is {2:^20} Metres.'
  14. introB = 'My name is {0:#<15}, I\'m {1:$>11} years old, and my height is {2:8^20} Metres.'
  15. introC = f'My name is {name}, I\'m {age} years old, and my height is {height} Metres.'
  16. print(intro1 % (name, age, height))
  17. print(intro2 % (name, age, height))
  18. print(intro3 % (name, age, height))
  19. print(intro4 % (name, age, height))
  20. print(intro5 % (name, age, height))
  21. print(intro6.format(name, age, height))
  22. print(intro7.format(height, age, name))
  23. print(intro8.format(age0=age, name0=name, height0=height))
  24. print(intro9.format(name, age, height))
  25. print(introA.format(name, age, height))
  26. print(introB.format(name, age, height))
  27. print(introC)

程序执行结果:

本文主要参照这篇博文,测试程序重新写。

Python 字符串格式化输出的3种方式

Python中字符串的学习的更多相关文章

  1. python中字符串的几种表达方式(用什么方式表示字符串)

    说明: 今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 ...

  2. python中字符串的四种表达方式

    今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 > ...

  3. 【Python从入门到精通】(九)Python中字符串的各种骚操作你已经烂熟于心了么?

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要. 干货满满,建议收藏,欢迎大 ...

  4. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  5. python中字符串的操作方法

    python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...

  6. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

  7. Python中字符串与字节之间相互转换

    Python中字符串与字节之间相互转换 ​ a = b"Hello, world!" # bytes object b = "Hello, world!" # ...

  8. 超详细!盘点Python中字符串的常用操作

    在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...

  9. Python中字符串有哪些常用操作?纯干货超详细

随机推荐

  1. selenium8中元素定位方式

    Selenium对网页的控制是基于各种前端元素的,在使用过程中,对于元素的定位是基础,只有准去抓取到对应元素才能进行后续的自动化控制,我在这里将对各种元素定位方式进行总结归纳一下. 这里将统一使用百度 ...

  2. 实验19:Frame-Relay

    实验16-1. 帧中继多点子接口 Ø    实验目的通过本实验,读者可以掌握如下技能:(1) 帧中继的基本配置(2) 帧中继的静态映射(3) 多点子接口的应用Ø     实验拓扑 实验步骤n  步骤1 ...

  3. (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离

    斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...

  4. C++零食:WTL中使用双缓冲避免闪烁

    双缓冲的原理可以这样形象的理解:把电脑屏幕看作一块黑板.首先我们在内存环境中建立一个"虚拟"的黑板,然后在这块黑板上绘制复杂的图形,等图形全部绘制完毕的时候,再一次性的把内存中绘制 ...

  5. FFMPEG学习----分离视频里的H.264与YUV数据

    #include <stdio.h> extern "C" { #include "libavcodec/avcodec.h" #include & ...

  6. Codeforces_831

    A.线性判断. #include<bits/stdc++.h> using namespace std; ] = {}; int main() { ios::sync_with_stdio ...

  7. 安利自己写的easy-clipboard库

    概述 clipboard.js 是一个非常好用的剪切板插件,但是随着前端框架的演变,用户与网页交互的方式越来越多,不仅限于点击事件了,并且在很多情况下,我们可能不需要它强制性自带的点击事件,所以我打算 ...

  8. FTP服务器配置http访问(配置nginx+ftp服务器)

    一.搭建nginx服务器 先安装nginx服务器 # yum install nginx -y 启动nginx服务 # systemctl start nginx 浏览器访问:http://192.1 ...

  9. 3,HDFS原理

    1,HDFS体系结构 ··· HDFS是采用master/slaves即主从结构模型来管理数据的.这种模型主要由四部分组成,分别是Client.NameNode.DataNode.SecondaryN ...

  10. Go语言基础之接口(面向对象编程下)

    1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...