python格式化输入_「format」Python3学习3 格式化 % format - seo实验室

format

一、%格式化

1.整数输出

%d # 十进制

%o # 八进制

%x # 十六进制

print("%d"%23) # 23

print("%o"%23) # 27

print("%x"%23) # 17

2.浮点数输出

%f: 默认保留小数点后6位

%.2f 保留两位小数 四舍五入

%e: 科学计数法,默认保留小数点后6位

%g: 保证6位有效数字的前提下用小数表示,否则用科学计数法print("%f"%2.3333) #2.333300

print("%.2f"%2.8888) #2.89 四舍五入

print("%e"%2.3333) #2.333300e+00

print("%.3e"%2.3333) #2.333e+00

print("%g"%2222.3333) #2222.33

print("%g"%22888822.3333) #2.28888e+07

print("%.7g"%2222.8888) #2222.889 .7是有效数字的个数

print("%.3g"%2222.3333) #2.22e+03

3.字符串输出

print("%s" % "hello everyone") # hello everyone

print("%65s" % "hello everyone") # 右对齐,左侧空格补位

print("%-65s" % "hello everyone") #左对齐,右侧空格补位

print("%.5s" % "hello everyone") #取前5个字符

print("%10.4s" % "hello everyone") #10位占位符,取4个字符右对齐 hell

print("%-10.4s" % "hello everyone") # hell

二、format

1.位置匹配

1.1 不带编号

v1 = '{} {}'.format('hello','everyone')

print(v1) # hello everyone

1.2 带数字编号(顺序可变)

v2 = '{0} {1}'.format('hello','everyone')

print(v2) # hello everyone

v3 = '{1} {0} {0}'.format('hello','everyone')

print(v3) # everyone hello hello

1.3 关键字

v4 = '{a} {b}'.format(a = 'hello',b = 'everyone')

print(v4) # hello everyone

v5 = '{a} {b} {b}'.format(a = 'hello',b = 'everyone')

print(v5) # hello everyone everyone

1.4 特殊

v6 = '{1} {0} {1}'.format(*'mnz')

print(v6) # n m n

d1 = {'a1' : 21, 'b1' : 34}

v7 = 'test1:{a1}, test2:{b1}'.format(**d1)

print(v7) # test1:21, test2:34

2.格式转换

# 将整数转换成对应的unicode字符

print('{:c}'.format(21016)) # 刘

# 十进制整数

print('{:d}'.format(20)) # 20

# 二进制整数

print('{:b}'.format(5)) # 101

# 八进制

print('{:o}'.format(23)) # 27

# 十六进制

print('{:x}'.format(23)) # 17

# 科学计数法

print('{:e}'.format(20)) # 2.000000e+01

print('{:g}'.format(20.34)) # 20.34

print('{:g}'.format(33320.34)) # 33320.3

print('{:.3g}'.format(33320.34)) # 3.33e+04

# n:d和g的合并

print('{:n}'.format(20)) # 20

print('{:n}'.format(20.23)) # 20.23

print('{:n}'.format(33320.23)) # 33320.2

print('{:3n}'.format(33320.23)) # 33320.2

print('{:f}'.format(20)) # 20.000000

print('{:.2f}'.format(23.8877)) #23.89

#数字乘100%,默认6位小数

print('{:%}'.format(20)) # 2000.000000%

print('{:.2%}'.format(20)) # 2000.00%

3. 正负号显示

print("{:f} and {:f}".format(2.345, -2.345)) # 2.345000 and -2.345000

print("{:+f} and {:f}".format(2.345, -2.345)) # +2.345000 and -2.345000

print("{:-f} and {:-f}".format(2.345, -2.345)) # 2.345000 and -2.345000

4. 对齐及位数不全

print("{} or {}".format('hello', 'everyone')) #左对齐 < 默认

print("{:20s} or {:>20s}".format('hello', 'everyone'))

print("{:20} or {:>20}".format('hello', 'everyone'))

print("{:^20} or {:^20}".format('hello', 'everyone'))

"""

输出:

hello or everyone

hello or everyone

hello or everyone

hello or everyone

"""

print("{:<20}".format('everyone')) # < 左对齐

print("{:>20}".format('everyone')) # > 右对齐

print("{:^20}".format('everyone')) # ^ 居中

print("{:*^21}".format('everyone')) # 填充

"""

输出:

everyone

everyone

everyone

******everyone*******

"""

print("{:0=20}".format(5123.12)) # = 只能用于数字,进行数字的补充

print("{:0<20}".format(5123.12))

print("{:0^20}".format(5123.12))

print("{0:>20.2f}".format(2.3456))

"""

输出:

00000000000005123.12

5123.120000000000000

0000005123.120000000

2.35

"""

5. 通过下标或key值匹配参数

c1 = [2, 3, 4]

c2 = [5, 6, 7]

print('{} {} {}'.format(c1[0],c1[1],c1[2])) # 2 3 4

print('{0[1]} {0[2]} {1[1]}'.format(c1, c2)) # 3 4 6

6. 逗号分隔

print('{:,}'.format(11556677842)) #11,556,677,842

相关阅读

1、在网上搜索下载“ChipGenius”,安装完成后运行该软件,程序会自动检测U盘的各项参数,从中找到U盘芯片型号。

2、在网上下载与芯

说明

1、调用方法 : new XMLFormat(xml).format();2、该类不引用其他第三方jar包.3、支持对缩写< />进行格式化。本文参考博文:ht

python2,3共存:第一节:python基本概念:Python交互模式,它的提示符是>>>基本命令:exit()python基本语言:1.          .py

目录:

Informatica基础系列(零)——前言

Informatica基础系列(一)——Helloworld

Informatica基础系列(二)——更新策略转换

问题

在开发的时候一段字符串的中间某一部分是需要可变的 比如一个Textview需要显示”XXX用户来自 上海 年

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,字符串格式化可以使用三种方式: 1. 百分号(%格式化字符串 这是Python早期版本中使用的一种格式化字符串的方式,类似于C语言中的printf函数。它的基本语法为: ```python string % value ``` 其中,string是要格式化的字符串,value是要替换的值。可以使用占位符来指定要替换的值的类型和格式,例如: - %s:字符串 - %d:整数 - %f:浮点数 例如,将一个字符串中的占位符替换为指定的值: ```python name = "Alice" age = 20 height = 1.65 info = "My name is %s, I'm %d years old, and my height is %.2f meters." % (name, age, height) print(info) ``` 输出结果为:My name is Alice, I'm 20 years old, and my height is 1.65 meters. 2. format()方法格式化字符串 这是Python中推荐使用的一种格式化字符串的方式,它的基本语法为: ```python string.format(value1, value2, ...) ``` 其中,string是要格式化的字符串,value1、value2等是要替换的值。可以使用占位符{}来指定要替换的值的类型和格式,例如: - {}:自动编号 - {0}、{1}:手动编号 - {:s}:字符串 - {:d}:整数 - {:.2f}:浮点数 例如,将一个字符串中的占位符替换为指定的值: ```python name = "Alice" age = 20 height = 1.65 info = "My name is {}, I'm {} years old, and my height is {:.2f} meters.".format(name, age, height) print(info) ``` 输出结果为:My name is Alice, I'm 20 years old, and my height is 1.65 meters. 3. f-string格式化字符串 这是Python 3.6及以上版本中新增的一种格式化字符串的方式,它的基本语法为: ```python f"string {value}" ``` 其中,string是要格式化的字符串,value是要替换的值。可以在占位符中使用表达式来计算要替换的值,例如: - {expression}:表达式 - {name}:变量名 例如,将一个字符串中的占位符替换为指定的值: ```python name = "Alice" age = 20 height = 1.65 info = f"My name is {name}, I'm {age} years old, and my height is {height:.2f} meters." print(info) ``` 输出结果为:My name is Alice, I'm 20 years old, and my height is 1.65 meters.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值