如何使用python在字符串中连接'

Urm*_*rmi 0 python concatenation append

我想在字符串的末尾和开头添加'.我有

str1 = 'abc'
str2 = 'def'
strf = str1 + str2
print strf
Run Code Online (Sandbox Code Playgroud)

它给出了输出

   abcdef
Run Code Online (Sandbox Code Playgroud)

我需要 'abcdef'

我试过加入

'''.join(strf)
Run Code Online (Sandbox Code Playgroud)

我试图在这里实现的是变量将被传递给解析一个xml,其中strf是xml的路径,所以它在开头和结尾都有.doc = ET.parse(strf)

PLease建议什么是最好的解决方案.提前致谢.URMI

Ash*_*ary 6

一个字符串总是有引号,没有引号它不是一个字符串.

使用repr看行情, print总是显示字符串中没有报价:

>>> str1 = 'abc'
>>> str2 = 'def'
>>> print str1 + str2
abcdef                     #just a human friendly output, the string still have quotes
>>> print repr(str1 + str2)
'abcdef'
Run Code Online (Sandbox Code Playgroud)

另一种选择是字符串格式:

>>> strf = str1 + str2
>>> print "'{}'".format(strf)
'abcdef'
Run Code Online (Sandbox Code Playgroud)