首页 > 编程知识 正文

总结一些python字符串拼接,字符串拼接操作python

时间:2023-12-28 11:56:51 阅读:327884 作者:IZIN

本文目录一览:

python字符串连接的几种方式总结

1、相加

website = 'python' + 'tab' + '.com'

2、%

'my name is %s,now %d years old' % ('liming',27)

3、{}.format

'myname is {0},now {1} years old'.format('liming','27')

python字符串常用方法

python字符串常用方法

1. Python字符串拼接(包含字符串拼接数字)

2. Python截取字符串(字符串切片)

3. Python 的len()函数:获取字符串长度或字节数

4. Python split()方法:分割字符串

5. Python join()方法:合并字符串

6. Python count()方法:统计字符串出现的次数

7. Python find()方法:检测字符串中是否包含某子串

8. Python index()方法:检测字符串中是否包含某子串

9. Python字符串对齐方法(ljust()、rjust()和center())

10. Python startswith()和endswith()方法

11. Python字符串大小写转换(3种)函数

12. Python去除字符串中空格(删除指定字符)的3种方法

python 文本字符串接

python中有很多字符串连接方式,今天在写代码,顺便总结一下:

最原始的字符串连接方式:str1 + str2

python 新字符串连接语法:str1, str2

奇怪的字符串方式:str1 str2

% 连接字符串:‘name:%s; sex: ' % ('tom', 'male')

字符串列表连接:str.join(some_list)

第一种,想必只要是有编程经验的人,估计都知道,直接用 “+” 来连接两个字符串:

'Jim' + 'Green' = 'JimGreen'

第二种比较特殊,如果两个字符串用“逗号”隔开,那么这两个字符串将被连接,但是,字符串之间会多出一个空格:

'Jim', 'Green' = 'Jim Green'

第三种也是 python 独有的,只要把两个字符串放在一起,中间有空白或者没有空白:两个字符串自动连接为一个字符串:

'Jim''Green' = 'JimGreen'

'Jim' 'Green' = 'JimGreen'

第四种功能比较强大,借鉴了C语言中 printf 函数的功能,如果你有C语言基础,看下文档就知道了。这种方式用符号“%”连接一个字符串和一组变量,字符串中的特殊标记会被自动用右边变量组中的变量替换:

'%s, %s' % ('Jim', 'Green') = 'Jim, Green'

第五种就属于技巧了,利用字符串的函数 join 。这个函数接受一个列表,然后用字符串依次连接列表中每一个元素:

var_list = ['tom', 'david', 'john']

a = '###'

a.join(var_list) = 'tom###david###john'

其实,python 中还有一种字符串连接方式,不过用的不多,就是字符串乘法,如:

a = 'abc'

a * 3 = 'abcabcabc'

字符串拼接的多种方式

方法一:+

s1="hello"

s2="world"

s3=s1+s2

print(s3)

方法二:join

str.join(sequence)

sequence -- 要连接的元素序列。

示例:

s1="-"

s2=['hello','world']

s3=s1.join(s2)

print(s3)

方法三:用%符号拼接

s1="hello"

s2="world"

s3="%s-%s"%(s1,s2)

print(s3)

方法四:format连接

s1="hello"

s2="world"

s3="{0}-{1}".format(s1,s2)

print(s3)

方法五:string模块的Template

from string import Template

fruit1 ="apple"

fruit2 ="banana"

fruit3 ="pear"

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

print(str.safe_substitute(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3))

print(str.safe_substitute(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3) )

效率比较:+号和join

结论:join的性能远高于+

原因:1)使用 + 进行字符串连接的操作效率低下,是因为python中字符串是不可变的类型,使用 + 连接两个字符串时会生成一个新的字符串,生成新的字符串就需要重新申请内存,当连续相加的字符串很多时(a+b+c+d+e+f+...) ,效率低下就是必然的了。2)join使用比较麻烦,但对多个字符进行连接时效率高,只会有一次内存的申请。而且如果是对list的字符进行连接的时候,这种方法必须是首选

示例佐证:

import time

def decorator(func):

    def wrapper(*args, **kwargs):

        start_time = time.time()

        func()

        end_time = time.time()

        print(end_time - start_time)

    return wrapper

@decorator

def method_1():

    s = ""

    for i in range(1000000):

        s += str(i)

@decorator

def method_2():

    l = [str(i) for i in range(1000000)]

    s = "".join(l)

method_1()

method_2()

结果:

1.940999984741211

0.2709999084472656

python字符串怎么和整数连接?

1、在python中完成字符串和数字的拼接,可以使用内置函数str()。

2、在python中如果直接对字符串和数字进行拼接,会发生报错。

3、使用内置函数str()转换为类型。

4、使用str()对数值转化为类型之后,可以正常运行。

5、在print()中使用逗号分隔打印数据,也可以解决字符串和数值连接的问题。

python怎么拼接字符串

python拼接字符串一般有以下几种方法:1.直接通过(+)操作符拼接: 输出结果:Hello World! 使用这种方式进行字符串连接的操作效率低下, 因为python中使用 + 拼接两个字符串时会生成一个新的字符串, 生成新的字符串就需要重新申请内存,...

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。