平台:win10 x64+Python3.7.0

先了解下——Python3 字符串格式化

Python字符串的格式化方法分为两种,分别为占位符(%)和format方式。

占位符方式在Python2.x中用的比较广泛,format方式在Python3.x使用的更加广泛。

一 、占位符(%)

%d

 age = 29
print("my age is %d" %age)
#my age is 29

实例一

 %s

 name = "makes"
print("my name is %s" %name)
#my name is makes

实例二

 %f

 print("%6.3f" % 2.3)
#2.300
print("%f" %2.3)
#2.300000

实例三

二、 format方法

位置映射:

 print("{}:{}".format('192.168.0.100'
 print("{}:{}".format('192.168.0.100',8080))
#192.168.0.100:8080

实例一

关键字映射:

 print("{server}{1}:{0}".format(8080,'192.168.1.100',server='Web Server Info :'))
#Web Server Info :192.168.1.100:8080

实例一

元素访问:

 print("{0[0]}.{0[1]}".format(('baidu','com')))
#baidu.com

实例一

填充对齐:

^、<、>分别是居中、左对齐、右对齐

 print("{0}*{1}={2:0>2}".format(3,2,2*3))
#3*2=06 print("{:*^30}".format('centered'))
#***********centered***********

实例一

 for i in range(1,10):
a = 1
while a <= i:
print("{0}*{1}={2:0>2}".format(a,i,a*i),end="\t")
a +=1
print() """
1*1=01
1*2=02 2*2=04
1*3=03 2*3=06 3*3=09
1*4=04 2*4=08 3*4=12 4*4=16
1*5=05 2*5=10 3*5=15 4*5=20 5*5=25
1*6=06 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=07 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=08 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=09 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
"""

实例二:九九乘法表

精度设置:

 print("{:.3f}".format(2.1415))
#2.142
print("{:.10f}".format(3.1415))
#3.1415000000

实例一

三、 解决print('{:15}|{:^9}|{:^9}'.format('', 'lat.', 'long.'))

{:15}——位置映射,相当于{0:15},对应于format中的'',15为字符宽度

|——字符串间的分隔符

{:^9}——位置映射,相当于{1:^9},对应于format中的'lat.',^为居中对齐,9为字符宽度

|——字符串间的分隔符

{:^9}——位置映射,相当于{2:^9},对应于format中的'long.',^为居中对齐,9为字符宽度

所以,输出为:               |  lat.   |  long.

print('{:15}|{:^9}|{:^9}'.format('', 'lat.', 'long.'))是什么意思?的更多相关文章

  1. 流畅的Python读书笔记(二)

    2.1 可变序列与不可变序列 可变序列 list. bytearray. array.array. collections.deque 和 memoryview. 不可变序列 tuple. str 和 ...

  2. 流畅的Python (Fluent Python) —— 第二部分01

    2.1 内置序列类型概览 Python 标准库用 C 实现了丰富的序列类型,列举如下. 容器序列 list. tuple 和 collections.deque 这些序列能存放不同类型的数据. 扁平序 ...

  3. 点读系列《流畅的python》

    第1章 python数据模型 python的写法是由背后的魔法方法实现的,比如obj[key],解释器实际调用的是obj.__getitem__(key) 作者把魔法方法叫做双下方法,因为有两个下划线 ...

  4. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...

  5. python输出格式化及函数format

    格式    描述%%     百分号标记%c     字符及其ASCII码%s     字符串%d     有符号整数(十进制)%u     无符号整数(十进制)%o     无符号整数(八进制)%x ...

  6. python的学习笔记01_2变量 常量 注释 用户交互 格式化输出

    变量是什么? 变量的作用  Variables are used to store information to be referenced and manipulated in a computer ...

  7. Python datetime.md

    datetime datetime模块包含了一些用于时间解析.格式化.计算的函数. Times 时间值由time类来表示, Times有小时, 分, 秒和微秒属性. 以及包含时区信息. 初始化time ...

  8. Python 读取照片的信息:拍摄时间、拍摄设备、经纬度等,以及根据经纬度通过百度地图API获取位置

    通过第三方库exifread读取照片信息.exifread官网:https://pypi.org/project/ExifRead/ 一.安装exifreadpip install exifread ...

  9. print格式化输出(format)

    一. print格式化输出,以及使用format控制 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越 ...

随机推荐

  1. Windows10关闭自动更新

    1 使用windows+r调出运行,输入:services.msc2 找到Windows update,右键选择属性:将启动类型从自动改为手动.3 使用windows+r调出运行,输入:gpedit. ...

  2. 如何查看k8s存在etcd中的数据(转)

    原文 https://yq.aliyun.com/articles/561888 一直有这个冲动, 想知道kubernetes往etcd里放了哪些数据,是如何组织的. 能看到,才有把握知道它的实现和细 ...

  3. kubernetes学习笔记之十:RBAC

    第一章.RBAC介绍 在Kubernetes中,授权有ABAC(基于属性的访问控制).RBAC(基于角色的访问控制).Webhook.Node.AlwaysDeny(一直拒绝)和AlwaysAllow ...

  4. node.js打印function

    var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...

  5. WPF 后台重写 DataTemplate

    /// <summary> /// 配置类 /// </summary> public static class GridControlDeploy { /// <sum ...

  6. Permanent Space 和 Heap Space

      JVM堆内存 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  7. leetcode31

    class Solution { public: void nextPermutation(vector<int>&nums) { int len = nums.size(); , ...

  8. Connection reset by peer原理解析

    “Connection reset by peer”代表什么?“Connection reset by peer”表示当前服务器接受到了通信对端发送的TCP RST信号,即通信对端已经关闭了连接,通过 ...

  9. (译)内存沉思:多个名称相关的神秘的SQL Server内存消耗者。

    原文出处:https://blogs.msdn.microsoft.com/sqlmeditation/2013/01/01/memory-meditation-the-mysterious-sql- ...

  10. mysql数据库中指定值在所有表中所有字段中的替换

    MySQL数据库: 指定值在数据库中所有表所有字段值的替换(存储过程): 1.写一个存储过程,查指定数据库中所有的表名: CREATE PROCEDURE init_replace(in orig_s ...