Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用re.sub()后删除单引号_Python_Regex - Fatal编程技术网

在python中使用re.sub()后删除单引号

在python中使用re.sub()后删除单引号,python,regex,Python,Regex,在用字符“^”替换字符串中的所有单词字符后,使用re.sub(“\w”,“^”,stringorphrase)剩下的是: >>> '^^^ ^^ ^^^^' 有没有办法去掉单引号,让它看起来更干净 >>> ^^^ ^^ ^^^^ 要删除所有出现的单引号,请执行以下操作: mystr = some_string_with_single_quotes answer = mystr.replace("'", '') 要仅删除字符串末尾的单引号,请执行以下操作:

在用字符“^”替换字符串中的所有单词字符后,使用
re.sub(“\w”,“^”,stringorphrase)
剩下的是:

>>> '^^^ ^^ ^^^^'
有没有办法去掉单引号,让它看起来更干净

>>> ^^^ ^^ ^^^^

要删除所有出现的单引号,请执行以下操作:

mystr = some_string_with_single_quotes
answer = mystr.replace("'", '')
要仅删除字符串末尾的单引号,请执行以下操作:

mystr = some_string_with_single_quotes
answer = mystr.strip("'")

希望这有助于使用
打印
语句。引号实际上不是字符串的一部分。

您确定它不是交互式提示或其他内容中的显示方式吗(并且字符串中没有实际的apost)

如果
实际上是字符串的一部分,并且是第一个/最后一个,则:

string = string.strip("'")
或:


您不能对单引号使用相同的子方法吗?这不是问题的答案,但您也可以(根据您的用例)删除带有
''的单词字符。join(如果c.isalpha(),则c代表s中的c)
,其中
s
是您的字符串(如果您将“word”定义为“字母”)。如果您这样做
repr(mystring)
,你得到了多少组引号?字符串本身没有单引号,它在用右引号替换im后,在begining和end中使用单引号。然后按照Mark Ransom的话打印出这个傻瓜!打印声明成功了,谢谢!在交互式promt中调用函数,oops lool.Thank,忘了打印语句,是通过交互式promt调用的。
string = string[1:-1] # lop ending characters off