在 Python 中從字符串中刪除引號 | D棧 - Delft Stack
1、replace()===將字符串中所有的引號都刪除
old_string= '"python"just"learn"' new_string=old_string.replace('"','') print("The original string is - {}".format(old_string)) #The original string is - "python"just"learn" print("The converted string is - {}".format(new_string)) #The converted string is - pythonjustlearn
2、strip() ===將字符串兩端的引號刪除
old_string= '"python"just"learn"' new_string=old_string.strip('"') print("The original string is - {}".format(old_string)) #The original string is - "python"just"learn" print("The converted string is - {}".format(new_string)) #The converted string is - python"just"learn
3、lstrip() === 將字符串開頭的引號刪除
old_string= '"python"just"learn"' new_string=old_string.lstrip('"') print("The original string is - {}".format(old_string)) #The original string is - "python"just"learn" print("The converted string is - {}".format(new_string)) #The converted string is - python"just"learn"
4、rstrip() === 將字符串結尾的引號刪除
old_string= '"python"just"learn"' new_string=old_string.rstrip('"') print("The original string is - {}".format(old_string)) #The original string is - "python"just"learn" print("The converted string is - {}".format(new_string)) #The converted string is - "python"just"learn
5、literal_eval()============不太理解,
此方法將測試一個 Python 字符或容器視圖表達式節點、Unicode 或 Latin-1 編碼的字符串。提供的字符串或節點只能由以下 Python 結構組成:字符串、數字、元組、列表、字典、布爾值等。它可以安全地測試包含不受信任的 Python 值的字符串,而不需要檢查值本身。
old_string= '"python learn"' new_string=eval(old_string) print("The original string is - {}".format(old_string)) #The original string is - "python"just"learn" print("The converted string is - {}".format(new_string)) #The converted string is - python learn
