简体   繁体   中英

switching between different quotation marks for output

how can i get my code to change the quotation marks of my output. i saw some references that mentioned json, but i think i need to write it myself. so i'll post the question and then my code:

Program: quote_me() Function

quote_me takes a string argument and returns a string that will display surrounded with added double quotes if printed

check if passed string starts with a double quote ("\\""), then surround string with single quotations if the passed string starts with single quote, or if doesn't start with a quotation mark, then surround with double quotations

Test the function code passing string input as the argument to quote_me()

[ ] create and test quote_me()

def quote_me (word):

if word == ("\'"):

    str(word).replace ("\'", '\"')

else:
    return word

print (quote_me(input ("what is the sentence: ")))

maybe i've misunderstood what is required as well, if that's the case, please do tell.

def quote_me(word):
    if word.startswith("\"") and word.endswith("\""):
        word = word.replace("\"","\'")
    elif word.startswith("\'") and word.endswith("\'"):
        word = word.replace("\'","\"")
    else:
        word = "\""+word+"\""
    return word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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