前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ruby vs. Python: 多行字符串的差异

Ruby vs. Python: 多行字符串的差异

作者头像
Python猫
发布2019-04-10 09:57:56
1.2K0
发布2019-04-10 09:57:56
举报
文章被收录于专栏:Python无止境Python无止境

在《你真的知道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:

代码语言:javascript
复制
=begin Multi Line Comment =end

And true to form, ruby provides several options for multiline strings.

  1. Just add return characters in a string
代码语言:javascript
复制
str = 'Multi Line String'
  1. Use a heredoc
代码语言:javascript
复制
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.

  1. Use the % operator
代码语言:javascript
复制
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:

代码语言:javascript
复制
''' 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猫 公众号后台与我分享。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档