新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

Python字符串去掉引号的有哪些方法,Python字符串去掉引号

发布时间:2023-09-29 04:16:21

Python字符串去掉引号的有哪些方法

Python中可使用以下方法去掉字符串中的引号:

  1. 使用strip()方法去掉字符串两边的引号:
string = '"Hello, World!"'
new_string = string.strip('"')
print(new_string)  # Output: Hello, World!
  1. 使用切片去掉字符串的引号:
string = '"Hello, World!"'
new_string = string[1:⑴]
print(new_string)  # Output: Hello, World!
  1. 使用replace()方法将引号替换为空字符串:
string = '"Hello, World!"'
new_string = string.replace('"', '')
print(new_string)  # Output: Hello, World!

请注意,以上方法只能去掉字符串两边的引号,如果字符串中间还包括引号,需要根据具体情况进行处理。