在字符串中加上引号

时间:2017-11-22 20:33:02

标签: python string python-3.x file

def add_div(filename, caption):

    test = str(filename)
    return ('<div><img src=' + test + '><br><p>' + caption + '</p></div>')


def add_body(image_dict, s, order = None):
    '''(dict of {str:list of str}, str, list) -> str

    If the third parameter is passed, then the filenames
    included in the body should only be those in the list and should be added
    in the same order as they are listed in the list. '''
    new = ''
    s = '<html><head></head>'

    while order is None:
       for (key, value) in image_dict.items():
           new  += add_div(str(key), str(value[2]))
       return (s + '<body><div id="slideshow">'  + new + '</body>'+ '</html>')

add_body函数的输出是:     enter image description here

如何在图片/ skater.jpg周围加上引号?

这就是文件的样子 enter image description here

4 个答案:

答案 0 :(得分:2)

您有两个不同的选项:

1)使用双引号

print("Hey that's pretty cool!")

2)转义单引号

print('Hey that\'s pretty cool!')

答案 1 :(得分:0)

您可以在串联的字符串中包含引号,如下所示:

def add_div(filename, caption):

    test = str(filename)
    return ('<div><img src="' + test + '"><br><p>' + caption + '</p></div>')

答案 2 :(得分:0)

quotation定义使用一种string,为包含的quotes使用另一种类型:

>>> "bob said: 'hello'"
"bob said: 'hello'"
>>> "I said 'yo bob, how u doing?' in reply"
"I said 'yo bob, how u doing?' in reply"

为了解决您的问题,只需将第一个return中的function语句更改为:

return ('<div><img src="' + test + '><br><p>'" + caption + '</p></div>')

请注意,最后,return语句中的括号不是必需的,因为return不是functionmethod

答案 3 :(得分:0)

答案很好,但另一种方法是使用\

来转义单引号或双引号

示例:

# this is the same as
# s = "'"
s = '\''
print(s)

#this is the same as
# s = '"'
s = "\""
print(s)