问答详情

python中如何实现字符串拼接?

4008次观看
标签: 字符串 如何实现 python
老师回答

1、加号法

使用简单直接,但这种方法效率低。

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

2、逗号法

字符串之间会多出一个空格。

str_a = 'python'

print('hello', str_a, '!')

输出

hello python !

3、直接拼接法

Python独有拼接法,只能用于字符串的拼接,不能用于变量拼接。

#code

print('abc''xyz')

#output

adcxyz

4、格式化法:使用%或者format进行拼接

>>> text1 = "Hello"

>>> text2 = "World"

>>> "%s%s"%(text1,text2)

'HelloWorld'

5、join函数法

listStr = [ 'python' , 'tab' , '.com' ]

website = ''.join(listStr)

6、多行字符串拼接法

>>> text = ('666'

'555'

'444'

'333')

>>> print(text)

666555444333

>>> print (type(text))

免费直播

    精选课程
    相关推荐

    注册电脑版

    版权所有 2003-2020 广州环球青藤科技发展有限公司