Python:如何插入特殊ch

2024-04-26 23:56:15 发布

您现在位置:Python中文网/ 问答频道 /正文

Stackoverflow is best 

我想做:

"""Stackoverflow is best""" 

如何用regex插入特殊字符,或者有其他方法吗?你知道吗


Tags: 方法isstackoverflowregexbest特殊字符
3条回答

试试这个

str1 = '"""Stackoverflow is best"""' # you can interchange the double and single quotes in python 
print str1

使用单引号:

foo = '"""Stackoverflow is best"""' 

或者可以使用转义字符:

foo = "\"\"\"Stackoverflow is best \"\"\""

创建foo不需要regex。你知道吗

我想你想要这样的东西

>>> foo = 'Stackoverflow is best '
>>> import re
>>> foo = re.sub(r'^\s*|\s*$', r'"""', foo)
>>> foo
'"""Stackoverflow is best"""'

相关问题 更多 >