python3_在 Python 中从字符串中删除引号

在 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

 

posted @ 2022-02-26 14:26  小鱼小鱼hi  阅读(2026)  评论(0编辑  收藏  举报