簡體   English   中英

替換字符串中的單引號和雙引號

[英]replace single quotes and double quotes from a string

我正在嘗試學習 python 並且遇到了一個問題,即我遇到了需要替換單引號和雙引號的字符串。 我的目標是將它們拆分為一個列表,但由於其他原因仍保留單引號和雙引號。 我嘗試了以下但收到錯誤

s=("I said, "hello" it's mine".replace(""",'`')).replace("'","^")
print(s)

File "<ipython-input-58-a5276888c42f>", line 1
    s=("I said, "hello" it's mine".replace(""",'`')).replace("'","^")
                     ^
SyntaxError: invalid syntax

只需使用反斜杠來轉義雙引號。

s = "I said, \"hello\" it's mine"

或者使用單引號字符串並轉義單引號。 (這是規范表示。)

s = 'I said, "hello" it\'s mine'

或者使用三引號 - 單引號或雙引號。

s = """I said, "hello" it's mine"""
s = '''I said, "hello" it's mine'''

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM