Python字符串拼接

在Python的实际开发中,很多都需要用到字符串拼接,python中字符串拼接有很多,今天总结一下:

  • 用+符号拼接
  • 用%符号拼接
  • 用join()方法拼接
  • 用format()方法拼接
  • 用string模块中的Template对象

如果还有其他方法,欢迎补充。 
例子:

fruit1 = 'apples'
fruit2 = 'bananas'
fruit3 = 'pears'

要求: 
输出字符串’There are apples, bananas, pears on the table’

1. 用+符号拼接

用+拼接字符串如下:

 str = 'There are'+fruit1+','+fruit2+','+fruit3+' on the table' 

该方法效率比较低,不建议使用

2. 用%符号拼接

用%符号拼接方法如下:

 str = 'There are %s, %s, %s on the table.' % (fruit1,fruit2,fruit3) 

除了用元组的方法,还可以使用字典如下:

 str = 'There are %(fruit1)s,%(fruit2)s,%(fruit3)s on the table' % {'fruit1':fruit1,'fruit2':fruit2,'fruit3':fruit3} 

该方法比较通用

3. 用join()方法拼接

join()`方法拼接如下

 temp = ['There are ',fruit1,',',fruit2,',',fruit3,' on the table']
''.join(temp)

该方法使用与序列操作

4. 用format()方法拼接

用format()方法拼接如下:

4. 用format()方法拼接

用format()方法拼接如下:

 str = 'There are {}, {}, {} on the table'
str.format(fruit1,fruit2,fruit3)

还可以指定参数对应位置:

 str = 'There are {2}, {1}, {0} on the table'
str.format(fruit1,fruit2,fruit3) #fruit1出现在0的位置

同样,也可以使用字典:

 str = 'There are {fruit1}, {fruit2}, {fruit3} on the table'
str.format(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3)

5. 用string模块中的Template对象

用string模块中的Template对象如下:

 from string import Template
str = Template('There are ${fruit1}, ${fruit2}, ${fruit3} on the table') #此处用的是{},别搞错了哦
str.substitute(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3) #如果缺少参数,或报错如果使用safe_substitute()方法不会
str.safe_substitute(fruit1=fruit1,fruit2=fruit2)
#输出'There are apples, bananas, ${fruit3} on the table'

总结

拼接的方法有多种,不同场合下使用不同的方法,个人比较推荐%、format()方法,简单方便。

python字符串拼接的更多相关文章

  1. Python字符串拼接、格式化输出、深浅复制

    1.Python字符串拼接:方法挺多.挺好用的.灵活使用可使代码简洁.可读性好. #1.用4种方法,将列表li = ['I','python','like'], #里面的单词拼成: I**like** ...

  2. python—字符串拼接三种方法

    python—字符串拼接三种方法   1.使用加号(+)号进行拼接 字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字 ...

  3. Python字符串拼接的6种方法(转)

    add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 原文:http://www.cnblogs.com/bigtreei/p/7892113.html 1. ...

  4. Python字符串拼接的6种方法

    如有其他字符串拼接方法 欢迎留言提出哦 (示例版本为Py2) 1. 加号 第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此直接用 “+” 来连接两个字符串 ...

  5. Python 字符串拼接、格式化输出、深浅复制

    拼接:"+"号(同类型可拼接) >>>li = [1,2] >>>li + li [1,2,1,2] >>>li*2 [1,2 ...

  6. 关于python字符串拼接的几种方法

    当时看完python的基本语法后 给朋友写了个美元概率换算 写完后拼接结果时候 发现压根不知道python怎么拼接字符串 看了些资料自己做了个总结 首先就是和JavaScript一样的拼接方式 nam ...

  7. Python 字符串拼接 sql ,造成 sql 注入例子

    简单的 userinfo 表 字符串拼接 sql import pymysql # 测试环境的数据库连接 conn = pymysql.connect(host='192.168.0.214', po ...

  8. python字符串拼接相关

    #字符串拼接#str.join(元组.列表.字典.字符串) 之后生成的只能是字符串.str = "-";seq = ("a", "b", & ...

  9. 可能是最全面的 Python 字符串拼接总结

    来源: 枫恋寒 链接: https://segmentfault.com/a/119000001.png"font-size: 12px;"> 在 Python 中字符串连接 ...

随机推荐

  1. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  2. python的类和对象(类的静态字段)

    转自:http://www.cnblogs.com/Eva-J/p/5044411.html 什么是静态字段 在开始之前,先上图,解释一下什么是类的静态字段(我有的时候会叫它类的静态变量,总之说的都是 ...

  3. IntelliJ IDEA配置

    1.取消idea默认打开工程: 2.导出,导入配置. 导出和导入配置好处:对IDEA配置好后导出配置,方便以后重装电脑或者重装IDEA后可以导入之前配置好的配置.避免重复配置 导出配置:File Ex ...

  4. IOS开发中一些受用的编码和调试方法积累

    1.## 与 @# 在宏里面该怎样使用 ##的使用, 首先我们添加一个宏 #define LRWeakSelf(type) __weak typeof(type) weak##type = type; ...

  5. new和delete操作符

    C 语言中提供了 malloc 和 free 两个系统函数, 完成对堆内存的申请和释放.而 C++则提供了两个操作符 new 和 delete. 1. newnew 分配内存空间时,  分配内存空间大 ...

  6. hdoj:2051

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  7. Ubuntu 10.04下架设流媒体服务器

    Ubuntu 10.04下架设流媒体服务器 个人建议:使用DarwinStreamingSrvr5.5.5,因为DarwinStreamingSrvr6.0.3安装过程中有很多问题需要解决! 目前主流 ...

  8. 关于QT Graphics View开启OpenGL渲染后复选框、微调框等无法正常显示的问题

    之前学习QT Graphics View框架,除了基本的图元外,还可以通过QGraphicsProxyWidget类添加QT的基本Widget(如按钮.复选框.单选框等),常使用的场景类接口如下: Q ...

  9. Centos 下 JProfiler 9.1.1 安装 部署 及 使用

    JProfiler[1] 是一个商业授权的Java剖析工具,由EJ技术有限公司,针对的Java EE和Java SE应用程序开发的.它把CPU.执行绪和内存的剖析组合在一个强大的应用中.JProfil ...

  10. JS设计模式——单例模式剖析

    转载于原文地址:https://blog.csdn.net/q1056843325/article/details/52933426 举一个通俗的例子,在页面中点击登录按钮,弹出了一个登录浮窗,这个登 ...