简体   繁体   English

将引号放在字符串中

[英]Putting quotation marks inside a string

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>')

The output of add_body function is: add_body函数的输出为: 在此处输入图片说明

how do I get quotation marks around the word images/skater.jpg ? 我如何在images / skater.jpg一词周围加上引号?

this is what the file looks like 这是文件的样子 在此处输入图片说明

You have two separate options: 您有两个单独的选择:

1) Use double quotes 1)使用双引号

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

2) Escape the single quotation mark 2)转义单引号

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

You can include the quotation marks in the string that you are concatenating like this : 您可以像这样在连接的字符串中包括引号:

def add_div(filename, caption):

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

Use one type of quotation for the string definition and the other for the included quotes : string定义使用一种类型的quotation ,对包含的quotes使用另一种类型的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"

So to fix your problem, just change the return statement in the first function to: 因此,要解决您的问题,只需将第一个functionreturn语句更改为:

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

Note that as a final thing, the parenthesis in the return statement aren't required as return is a not a function or method . 请注意,最后,不需要return语句中的括号,因为return不是functionmethod

Good answers, but another method is to escape the single or double quotation mark with \\ 好的答案,但是另一种方法是使用\\来转义单引号或双引号\\

Example: 例:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM