python - 替换字符串中的单引号和双引号
问题描述
我正在尝试学习 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'''
推荐阅读
- vue.js - 无法使用 vuetify 删除 v-text-field 和标签之间的空白
- python - 我应该怎么做才能解决我在 python 中使用 dropna 和 fillna 的问题?
- node.js - 视图中的文件夹
- c# - 无论日期如何,如何设置特定的时间范围(例如从 07:00 到 12:00)
- apache-kafka - 计算 Kafka 分区以实现特定的吞吐量
- python - 地理编码超时。错误是由数据框表引起的吗?
- php - PHP - 将未定义的变量与空变量区分开来
- c++ - Google Jam 2018 参议院疏散
- python - 如何将包含文件名和信息的文件分别拆分为多个文件?
- r - R:如何读取一列,查找一个值并根据此搜索计算另一列中的某些内容?