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 @   小鱼小鱼hi  阅读(2037)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示