在 python 中使用替代字符串引用语法

发布于 2024-09-01 16:02:18 字数 908 浏览 4 评论 0原文

只是想知道......

我发现使用转义字符太分散注意力。我宁愿做这样的事情(控制台代码):

>>> print ^'Let's begin and end with sets of unlikely 2 chars and bingo!'^
Let's begin and end with sets of unlikely 2 chars and bingo!

注意字符串内的 ' ,以及这种语法如何没有问题,或者基本上所有情况下的其他内容。太糟糕了 markdown 还不能正确地为其着色,所以我决定

它。

当然, ^ 可以是任何其他字符,我不确定什么看起来/工作得更好。这对我来说听起来不错,不过。

可能其他一些语言已经有类似的解决方案。而且,也许 Python 已经有了这样的功能,只是我忽略了它。我希望情况确实如此。

但如果不是,那么以某种方式改变 Python 的解释器并能够选择任意(甚至标准化)语法来表示字符串会不会太困难?

我意识到有很多方法可以改变语句和整个语法一般为使用预编译器,但这要具体得多。我将这些路线中的任何一条称为“太难”。我其实并不需要这样做,所以我只是想知道。

Just wondering...

I find using escape characters too distracting. I'd rather do something like this (console code):

>>> print ^'Let's begin and end with sets of unlikely 2 chars and bingo!'^
Let's begin and end with sets of unlikely 2 chars and bingo!

Note the ' inside the string, and how this syntax would have no issue with it, or whatever else inside for basically all cases. Too bad markdown can't properly colorize it (yet), so I decided to <pre> it.

Sure, the ^ could be any other char, I'm not sure what would look/work better. That sounds good enough to me, tho.

Probably some other language already have a similar solution. And, just maybe, Python already have such a feature and I overlooked it. I hope this is the case.

But if it isn't, would it be too hard to, somehow, change Python's interpreter and be able to select an arbitrary (or even standardized) syntax for notating the strings?

I realize there are many ways to change statements and the whole syntax in general by using pre-compilators, but this is far more specific. And going any of those routes is what I call "too hard". I'm not really needing to do this so, again, I'm just wondering.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最后的乘客 2024-09-08 16:02:18

Python 使用 """ 或 ''' 作为分隔符

print '''Let's begin and end with sets of unlikely 2 chars and bingo'''

字符串中同时出现 3' 和 3" 的频率有多少

Python has this use """ or ''' as the delimiters

print '''Let's begin and end with sets of unlikely 2 chars and bingo'''

How often do you have both of 3' and 3" in a string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文