在《你真的知道Python的字符串是什么吗?》里,我们比较了 Python 多行字符串与Java的区别。有小伙伴说这只是语法的区别,他觉得并不重要。真是不重要吗?
Python 推崇使用优雅的解决方案,而且希望这种方案是唯一的。在多行字符串的实现上,我觉得其它编程语言都得向它好好学学,例如 Ruby。由于项目原因,我曾阅读过一些 Ruby 代码,那真是一种折磨。
今年10月,Github 发布了年度报告,看到 Ruby 从第五名逐年下滑到第十名,我觉得这“名副其实”吧。
今天,我给大家分享一篇英语短文,它主要比较了 Ruby 和 Python 在多行字符串的实现。这篇文章简洁易读,希望有助于你理解编程语言的“字符串”是什么。
原题:Ruby vs. Python: Multiline Comments and Strings
链接:http://t.cn/E20Xc9H
One of the primary differences between Ruby and Python is that Rubyists believe that there are many right ways to do anything and Pythonistas believe there is only one. Multiline comments and strings are a great example of this difference.
In ruby, multiline comments look like this:
=begin Multi Line Comment =end
And true to form, ruby provides several options for multiline strings.
str = 'Multi Line String'
str = <<-EOS Multi Line String EOS
EOS can be replaced with any string of characters. Whatever you use at the top must match what you use at the bottom.
str = %{ Multi Line String }
Pythonistas believe there should be only one correct way to do anything, so true to form, they have one way of doing all of these things. The triple quotation mark:
''' Multi Line Comment '''
str = ''' Multi Line String '''
The only difference between multiline comments and multiline strings is that strings get used and comments don’t.
以上就是今天的分享,希望对你有帮助。另外,如果你还知道其它编程语言,在字符串的实现上有何差异,欢迎在 Python猫
公众号后台与我分享。