James Walker
James Walker

Reputation: 35

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: enter image description here

how do I get quotation marks around the word images/skater.jpg ?

this is what the file looks like enter image description here

Upvotes: 0

Views: 1129

Answers (4)

Edwin van Mierlo
Edwin van Mierlo

Reputation: 2488

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)

Upvotes: 0

rahlf23
rahlf23

Reputation: 9019

You have two separate options:

1) Use double quotes

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

2) Escape the single quotation mark

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

Upvotes: 2

Joe Iddon
Joe Iddon

Reputation: 20414

Use one type of quotation for the string definition and the other for the included 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:

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.

Upvotes: 0

Neil Anderson
Neil Anderson

Reputation: 1355

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

Upvotes: 0

Related Questions