Python实现string字符串连接的方法总结【8种方式】

本文实例总结了Python实现string字符串连接的方法。分享给大家供大家参考,具体如下:

以下基于python 2.7版本,代码片段真实有效。

一. str1+str2

string类型 ‘+'号连接

>>> str1="one"
>>> str2="two"
>>> str1+str2
'onetwo'
>>>

二. str1,str2

string类型 ‘,'号连接成tuple类型

>>> str1="one"
>>> str2="two"
>>> str1 ,str2
('one', 'two')
>>> type((str1 ,str2))
<type 'tuple'>
>>>

三. 格式化字符串连接

string类型格式化连接

1.常见的格式化方式

>>> str1="one"
>>> str2="two"
>>> "%s%s"%(str1,str2)
'onetwo'

2.高级点的format 格式化

>>> "{test}_666@{data:.2f}".format(test="Land", data=10.1)
'Land_666@10.10'

3.鲜为人知的【%(word)typeprint函数格式化

>>> print "%(test)s666%(last)d" % {"test": "Land", "last": 101}
Land666101

四. str1 str2

string类型空格自动连接

>>> "one" "two"
'onetwo'

这里需要注意的是,参数不能代替具体的字符串写成
错误方式:

>>> str1="one"
>>> str2="two"
>>> str1 str2
 File "<stdin>", line 1
  str1 str2
      ^
SyntaxError: invalid syntax

五. str1 \ str2 \str3

string类型反斜线多行连接

>>> test = "str1 " \
... "str2 " \
... "str3"
>>> test
'str1 str2 str3'
>>>

六. M*str1*N

string类型乘法连接

>>> str1="one"
>>> 1*str1*4
'oneoneoneone'
>>>

七. join方式连接

string类型join方式连接list/tuple类型

>>> str1="one"
>>> list1=["a","b","c"]
>>> tuple1=("H","I","J")
>>> str1.join(list1)
'aonebonec'
>>> str1.join(tuple1)
'HoneIoneJ'

这里的join有点像split的反操作,将列表或元组用指定的字符串相连接;

但是值得注意的是,连接的列表或元组中元素的类型必须全部为string类型,否则就可能报如下的错误:

>>> list2=["a",2,"c",4.3]
>>> str1.join(list2)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected string, int found
>>>

join还有一个妙用,就是将所有list或tuple中的元素连接成string类型并输出;

>>> list1
['a', 'b', 'c']
>>> "".join(list1)
'abc'
>>> type("".join(list1))
<type 'str'>
>>>

八.列表推导方式连接

与join方式类似

>>> "".join(["Land" for i in xrange(3)])
'LandLandLand'
>>> "0".join(["Land" for i in xrange(2)])
'Land0Land'
>>>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

(0)

相关推荐

  • Python的string模块中的Template类字符串模板用法

    string.Template() string.Template()内添加替换的字符, 使用"$"符号, 或 在字符串内, 使用"${}"; 调用时使用string.substitute(dict)函数. 可以通过继承"string.Template", 覆盖变量delimiter(定界符)和idpattern(替换格式), 定制不同形式的模板. 代码: # -*- coding: utf-8 -*- import string templat

  • Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    去空格及特殊符号 s.strip().lstrip().rstrip(',') Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 连接字符串 #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print

  • python开发之字符串string操作方法实例详解

    本文实例讲述了python开发之字符串string操作方法.分享给大家供大家参考,具体如下: 在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号''和双引号""标示 strA = 'this is a string' strB = "this is a message!" #打印两个字符串 print("打印两

  • Python数据类型之String字符串实例详解

    本文实例讲述了Python数据类型之String字符串.分享给大家供大家参考,具体如下: String(字符串) 1.概述 字符串是以单引号或双引号括起来的任意文本,比如"abc",'xy'等等,请注意''或者""本身只是一种表示方式,并不是字符串的一部分. a.若字符串内部包含单引号又包含双引号怎么办? print('I\'m \"ok\"') 表示的字符串内容是: I'm "ok" 注意:转义字符\可以转义很多字符,比如\

  • Python 基础之字符串string详解及实例

    Python字符串(string) 详解 及 代码 Python的字符串可以使用单引号('), 双引号("), 三引号('''); 三引号(''')里面, 可以添加单引号和双引号, 也可以通过转义序列(\)添加; 字符串放在一起自动连接成为一个字符串; 字符串前面添加限定词R或r, 表示是自然字符串(nature string), 可以忽略里面的格式限制; 在物理行末尾添加"\", 可以连接下一个物理行; 括号, 方括号, 大括号也可以一定限度的扩充物理行; 具体参见代码注释

  • 详解python 字符串和日期之间转换 StringAndDate

    python 字符串和日期之间转换 StringAndDate           这里给出实现代码,直接可以使用.大家可以看下. 实例代码: ''''' Created on 2013-7-25 @author: Administrator ''' from datetime import datetime class StringAndDate(object): ''''' String to Date(datetime) or date to string ''' def stringTo

  • Python原始字符串(raw strings)用法实例

    本文实例讲述了Python原始字符串(raw strings)用法,分享给大家供大家参考.具体如下:   Python原始字符串的产生正是由于有正则表达式的存在.原因是ASCII 字符和正则表达式特殊字符间所产生的冲突.比如,特殊符号"\b"在ASCII 字符中代表退格键,但同时"\b"也是一个正则表达式的特殊符号,代表"匹配一个单词边界". 为了让RE 编译器把两个字符"\b"当成你想要表达的字符串,而不是一个退格键,你需要

  • python字符串string的内置方法实例详解

    下面给大家分享python 字符串string的内置方法,具体内容详情如下所示: #__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print(a) print("-------------------") a = 'Let\'s go' print(a) print("-------------------") print("hello"

  • Python列表(list)、字典(dict)、字符串(string)基本操作小结

    创建列表 复制代码 代码如下: sample_list = ['a',1,('a','b')] Python 列表操作 复制代码 代码如下: sample_list = ['a','b',0,1,3] 得到列表中的某一个值 复制代码 代码如下: value_start = sample_list[0] end_value = sample_list[-1] 删除列表的第一个值 复制代码 代码如下: del sample_list[0] 在列表中插入一个值 复制代码 代码如下: sample_li

  • python实现字典(dict)和字符串(string)的相互转换方法

    本文实例讲述了python实现string和dict的相互转换方法.分享给大家供大家参考,具体如下: 字典(dict)转为字符串(string) 我们可以比较容易的将字典(dict)类型转为字符串(string)类型. 通过遍历dict中的所有元素就可以实现字典到字符串的转换: for key, value in sample_dic.items(): print "\"%s\":\"%s\"" % (key, value) 字符串(string

随机推荐