Python 在字符串中加引号 def add_div(文件名、标题): test=str(文件名) 返回(“

Python 在字符串中加引号 def add_div(文件名、标题): test=str(文件名) 返回(“,python,string,python-3.x,file,Python,String,Python 3.x,File,”+标题+”) def添加正文(图像记录,顺序=无): ''”({str:list of str},str,list)->str 如果传递了第三个参数,则文件名 正文中应仅包括列表中的内容,并应添加这些内容 按照列表中列出的顺序排列。“” 新=“” s=“” 虽然订单为无: 对于图像_dict.items()中的(键、值): new+=add_div(str(键),str(值[2])) 返回(s+“”+新+“”+“”) add_body函数的输出为: 如何在images/skater.jpg

”+标题+”

) def添加正文(图像记录,顺序=无): ''”({str:list of str},str,list)->str 如果传递了第三个参数,则文件名 正文中应仅包括列表中的内容,并应添加这些内容 按照列表中列出的顺序排列。“” 新=“” s=“” 虽然订单为无: 对于图像_dict.items()中的(键、值): new+=add_div(str(键),str(值[2])) 返回(s+“”+新+“”+“”) add_body函数的输出为:

如何在images/skater.jpg这个词周围加引号

这就是文件的外观

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

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>')
def add_div(文件名、标题):
test=str(文件名)
返回(“
”+标题+”


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

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>')
def add_div(文件名、标题):
test=str(文件名)
返回(“
”+标题+”


字符串定义使用一种类型的
引号
,对包含的
引号
使用另一种类型:

def add_div(filename, caption):

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

因此,要解决您的问题,只需将第一个
函数中的
return
语句更改为:

>>> "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
语句中的括号不是必需的,因为
return
不是
函数
方法
使用一种类型的
引号
用于
字符串
定义,另一种类型用于包含的
引号

def add_div(filename, caption):

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

因此,要解决您的问题,只需将第一个
函数中的
return
语句更改为:

>>> "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
语句中的括号不是必需的,因为
return
不是
函数
方法
您有两个单独的选项:

1) 使用双引号

return ('<div><img src="' + test + '><br><p>'" + caption + '</p></div>')
2) 跳出单引号

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

您有两个单独的选项:

1) 使用双引号

return ('<div><img src="' + test + '><br><p>'" + caption + '</p></div>')
2) 跳出单引号

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

答案不错,但另一种方法是用
\

例如:

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

答案不错,但另一种方法是用
\

例如:

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