使用百分号拼接字符串:

例如:

msg='i am %s my hobby is...' %'abc'
print(msg)

如果需要用2个%s呢?就使用括号
例如:

msg='I am %s my hobby is %s' %('abc','DDD')  
print(msg)
返回结果:
i am abc my hobby is DDD

%s是万能的,可以接收数字、字符串、列表
缺点是可读性差

msg='I am %s my hobby is %s' %('abc',123)
print(msg)
msg='I am %s my hobby is %s' %('abc',[2,3,4])
print(msg)

%.<数字>s对字符串的截取宽度设置
例如:

msg='I am %.4s' %'abcdefg'
print(msg)
返回结果:
I am abcd

%d只接收数字
如果给字符串、列表等会报错

msg='I am %s my age is %d' %('abc',19)
print(msg)

%f接收浮点数

msg='Percent %f' %99.97654321234566
print(msg)
返回结果:
Percent 99.976543

%f保留小数点后2位

msg='Percent %.2f' %99.97654321888
print(msg)
返回结果:
Percent 99.98

%%显示百分比符号,使用2个%符号

例如:

msg="Percent %.2f%%" %99.97654321111
print(msg)

使用变量的格式化显示字符串
例如:

msg="I am %(name)s age %(age)d" %{"name":"alex","age":19}
print(msg)
返回结果:
I am alex age 19

%s的左右对齐,保留宽度
例如:
  %-<数字>s 表示左对齐
  %+<数字>s 表示右对齐

msg='I am %(name)-20s hobby is ...' %{"name":"abc"}
print(msg)
返回结果:
I am abc hobby is ... msg='I am %(name)+20s hobby is ...' %{"name":"abc"}
print(msg)
返回结果:
I am abc hobby is ...

********format格式化字符串********

#元组的形式为字符串一一提供

tpl="i am {}, age {},{}".format("seven",18,'alex')
print(tpl)

#字典的形式为字符串提供

tpl="I am {name},age {age}, really {name}".format(name="seven",age=18)
print(tpl) #个人最喜欢这种方式,简单、直接、清晰明了

#另一种写法:以字典的方式书写,但注意一定要使用**

tpl="i am {name}, age {age}, really {name}".format(**{"name":"seven","age":18})
print(tpl)  #**{"name":"seven","age":18}作用就是把键值对取出来转换成name="seven"的方式
tpl="i am {0[0]}, age{0[1]}, really {0[2]}".format([1,2,3],[11,22,33])
print(tpl)

说明:format除提供了字典的key-value形式之外,其余都以元组的形式提供,
  所以展开后得到元组:([1,2,3],[11,22,33])
  所以{0[0]}到元组([1,2,3],[11,22,33])取值就是[1,2,3]中的1;
  所以{0[1]}到元组([1,2,3],[11,22,33])取值就是[1,2,3]中的2;
  所以{0[2]}到元组([1,2,3],[11,22,33])取值就是[1,2,3]中的3

tpl="i am {:s}, age {:d}, money {:f}".format("seven",18,9999.1)
说明:
  {:s}代表字符串
  {:d}代表数字
  {:f}代表浮点数

#如果要传入列表,则要使用1个*
例如:

tpl="i am {:s}, age {:d}".format(*["serven",18])    
li=["seven",19]
tpl="i am {:s}, age {:d}".format(li)

说明:如果这样写,本身format会把li加入到元组或列表中,但是li本身就是个列表
这样就造成列表/元组中的列表,会产生奇怪的问题,所以此时要使用星号:

tpl="i am {:s}, age {:d}".format(*li)
使用*把列表中的值取出来,再放到format构造的列表或元组中

tpl="number: {:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,15.87623,2)
print(tpl)
返回结果:
number: 1111,17,15,f,F,1587.623000% :b 表示二进制
:o 表示八进制
:d 整形数字
:x 小写十六进制
:X 大写十六进制
:% 表示百分号

python之字符串拼接:%和format的更多相关文章

  1. python中字符串格式化%与.format

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  2. Python中字符串拼接的N种方法

    python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...

  3. Python 基础 字符串拼接 + if while for循环

    注释单行注释 #多行注释 ''' 三个单引号或者三个双引号 """ ''' 用三引号引住可以多行赋值 用户交互 input 字符串拼接 +  ""%( ...

  4. Python中字符串拼接的三种方式

    在Python中,我们经常会遇到字符串的拼接问题,在这里我总结了三种字符串的拼接方式:     1.使用加号(+)号进行拼接 加号(+)号拼接是我第一次学习Python常用的方法,我们只需要把我们要加 ...

  5. python格式化字符串Type Error: Format Requires Mapping 的问题

    最近几天 频繁看到有这种写法 BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s" 第一次看到的pythoner看到可能会有点懵逼 ...

  6. Python格式化字符串(f,F,format,%)

    # 格式化字符串: 在字符串前加上 f 或者 F 使用 {变量名} 的形式来使用变量名的值 year = 2020 event = 'Referendum' value = f'Results of ...

  7. py-day2-5 python 百分号字符串拼接

    #### 字符串格式化. # %s 代替任何的元素 (数字,字符串,列表··) print('I live %s crty' %'my') print('I live %s crty' %'[6,8, ...

  8. day14 Python百分号字符串拼接

    拼接 # -*- coding:utf8 -*- #%s字符串,%d数字msg = '%s am %s my %s is %s'% (2,"charon","pluto& ...

  9. Python 百分号字符串拼接

    # %s可以接收一切 %d只能接收数字 msg = 'i am %s my hobby is %s' %('lhf','alex') print msg msg2 = 'i am %s my hobb ...

随机推荐

  1. Chromium(Chrome) Sandbox Details

    What Sandbox Do? Sandbox leverages the OS-provided security to allow code execution that cannot make ...

  2. cmake中添加-fPIC编译选项方法

    合并openjpeg/soxr/vidstab/snappy等多个cmake库时,为了解决下述问题: relocation R_X86_64_32 against `.text' can not be ...

  3. 在ubuntu下装python3.6

    Ubuntu 14.04 and 16.04 If you are using Ubuntu 14.04 or 16.04, you can use J Fernyhough's PPA at htt ...

  4. Java中的“==操作符”和equals方法有什么区别

    Java中的"=="和equals方法究竟有什么区别? 1.==操作符 "=="操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的 ...

  5. 使用sqlplus创建Oracle表空间

    登录 dos窗口输入以下命令:sqlplus  回车 提示输入用户名——即创建数据库实例时的用户名 ,用户名输入:sys as sysdba 密码.........回车登录 查看数据文件位置 接下来, ...

  6. The type 'System.Object' is defined in an assembly that is not referenced

    记录一个错误,报 The type 'System.Object' is defined in an assembly that is not referenced,[System.Runtime] ...

  7. Windows Unity ARKit发布到IOS相关设置及错误解决

    Windows 版Unity安装: 考虑到在虚拟机中运行Unity比较卡,所以采用在Windows Unity上将项目发布好然后再复制到Mac虚拟机中通过XCode进行编译的方式. Unity版本为 ...

  8. java异常处理——finally相关

    示例程序1 public class EmbededFinally { public static void main(String args[]) { int result; try { Syste ...

  9. Odoo二次开发

    Odoo 点击进入

  10. Windows下安装MySQL5.7.18的方法

    准备: 操作系统:win7 下64位的zip版本的MySQL,路径:http://dev.mysql.com/downloads/mysql/ 我下的是最新版的MySQL,解压后,目录如下: 可以看到 ...