5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby → Python チートシート(文字列Stringクラス編)

Last updated at Posted at 2020-04-28

凡例

メソッド名(兼ドキュメントへのリンク)
【Ruby】例
【Python】対応する例(シンタックスや結果の違いがわかるようなもの)
対応ドキュメントのURL

  • ここでは変数 r = "ルビー" p = "パイソン" としています。
  • -∗- encoding: UTF-8 -∗- です。

INDEX

%

【Ruby】"%s vs %s"%[r, p] # => "ルビー vs パイソン"(※ 引数は配列)
【Python】"%s vs %s"%(r, p) # => "ルビー vs パイソン"(※ 引数はタプル)
https://docs.python.org/ja/3/library/stdtypes.html#printf-style-string-formatting

<<

【Ruby】r << p # r => "ルビーパイソン"
【Python】p += r # p => "パイソンルビー"

<=>

【Python】該当なし

*

【Ruby】r * 2 # => "ルビールビー"
【Python】p * 2 # => "パイソンパイソン"

+@

【Python】該当なし

+

【Ruby】r + p # => "ルビーパイソン"
【Python】p + r # => "パイソンルビー"

-@

【Python】該当なし

===

【Ruby】r === "ルビー" # => true
【Python】p == "パイソン" # => True

==

【Ruby】r == p # => false
【Python】p == r # => False

=~

【Ruby】r =~ /ビ/ # => 1
【Python】re.search(r"ビ", r) # => <re.Match object; span=(1, 2), match="ビ">
https://docs.python.org/ja/3/library/re.html#re.search

[]=

【Python】該当なし

[]

【Ruby】r[0] # => "ル" r[0,2] # => "ルビ" r[0..1] # => "ルビ" r[0...2] # => "ルビ"
【Python】r[0] # => "ル" r[0:3] # => "ルビ"

ascii_only?

【Ruby】r.ascii_only? # => false
【Python】p.isascii() # => False
https://docs.python.org/ja/3/library/stdtypes.html#str.isascii

b

【Python】該当なし

bytes

【Ruby】r.bytes # => [227, 131, 171, 227, 131, 147, 227, 131, 188]
【Python】list(r.encode("utf-8")) # => [227, 131, 171, 227, 131, 147, 227, 131, 188]
https://docs.python.org/ja/3/library/stdtypes.html#list
https://docs.python.org/ja/3/library/stdtypes.html#str.encode

bytesize

【Ruby】r.bytesize # => 9
【Python】len(r.encode("utf-8")) # => 9

byteslice

【Ruby】r.byteslice(0) # => "\xE3" r.byteslice(0,3) # => "ル" r.byteslice(0..2) # => "ル" r.byteslice(0...3) # => "ル"
【Python】p[0] # => "パ"

capitalize

【Ruby】"rubyPython".capitalize # => "Rubypython"
【Python】"rubyPython".capitalize() # => "Rubypython"
https://docs.python.org/ja/3/library/stdtypes.html#str.capitalize

casecmp?

【Ruby】"ruby".casecmp?("RUBY") # => true "RUBY".casecmp?("ruby") # => true
【Python】"python".casefold() == "PYTHON" # => False "PYTHON".casefold() == "python" # => True
https://docs.python.org/ja/3/library/stdtypes.html#str.casefold

casecmp

【Python】該当なし

center

【Ruby】r.center(9) # => ":::ルビー:::"
【Python】p.center(9) # => ":::ルビー:::"
https://docs.python.org/ja/3/library/stdtypes.html#str.center

chars

【Ruby】r.chars # => ["ル", "ビ", "ー"]
【Python】list(p) # => => ["パ", "イ", "ソ", "ン"]
https://docs.python.org/ja/3/library/stdtypes.html#list

chomp

【Ruby】"ルビー\n".chomp # => "ルビー"
【Python】"パイソン\n".rstrip() # => "パイソン"
https://docs.python.org/ja/3/library/stdtypes.html#str.rstrip

chop

【Ruby】r.chop # => "ルビ"
【Python】p[:-1] # => "パイソ"
https://docs.python.org/ja/3/reference/expressions.html#slicings

chr

【Ruby】r.chr # => "ル"
【Python】p[0] # => "パ"
https://docs.python.org/ja/3/reference/expressions.html#slicings

clear

【Ruby】r.clear # => ""
【Python】p = "" # => ""

codepoints

【Ruby】r.codepoints # => [12523, 12499, 12540]
【Python】[ord(a) for a in r] # => [12523, 12499, 12540]
https://docs.python.org/ja/3/library/functions.html#ord

concat

【Ruby】r.concat(12497) # => "ルビーパ"
【Python】r += chr(12497) # => "ルビーパ"
https://docs.python.org/ja/3/library/functions.html#chr

count

【Ruby】p.count("パン") # => 2
【Python】p.count("パン") # => 0
https://docs.python.org/ja/3/library/stdtypes.html#str.count

crypt

【Python】該当なし

delete

【Ruby】p.delete("パソ") # => "イン"
【Python】re.sub(r"パ|ソ", "", p) # =>"イン"
https://docs.python.org/ja/3/library/re.html#re.sub

delete_prefix

【Ruby】p.delete_prefix("パイ") # => "ソン"
【Python】re.sub(r"^パイ", "", p) # => "ソン"
https://docs.python.org/ja/3/library/re.html#re.sub

delete_suffix

【Ruby】r.delete_suffix("ー") # => "ルビ"
【Python】re.sub(r"ー$", "", r) # => "ルビ"
https://docs.python.org/ja/3/library/re.html#re.sub

downcase

【Ruby】"RUBY".downcase # => "ruby"
【Python】"PYTHON".lower() # => "python"
https://docs.python.org/ja/3/library/stdtypes.html#str.lower

dump

【Python】該当なし

each_byte

【Ruby】r.each_byte.map{|b| b} # => [227, 131, 171, 227, 131, 147, 227, 131, 188]
【Python】[i for i in r.encode("utf-8")] # => [227, 131, 171, 227, 131, 147, 227, 131, 188]
https://docs.python.org/ja/3/library/stdtypes.html#str.encode

each_char

【Ruby】r.each_char{|c| print "|%s|"%(c)} # => |ル||ビ||ー|
【Python】[print("|%s|"%(c), end="") for c in r] # => |ル||ビ||ー|

each_codepoint

【Ruby】r.each_codepoint.to_a # => [12523, 12499, 12540]
【Python】[ord(c) for c in r] # => [12523, 12499, 12540]
https://docs.python.org/ja/3/library/functions.html#ord

each_grapheme_cluster

【Python】該当なし

each_line

【Ruby】"ruby\npython\n".each_line.to_a # => ["ruby\n", "python\n"]
【Python】"ruby\npython\n".splitlines(keepends=True) # => ["ruby\n", "python\n"]
https://docs.python.org/ja/3/library/stdtypes.html#str.splitlines

empty?

【Ruby】"".empty? # => true
【Python】len("") == 0 # => True
https://docs.python.org/ja/3/library/functions.html#len

encode

【Ruby】r.encode("eucjp") # => "\x{A5EB}\x{A5D3}\x{A1BC}"
【Python】r.encode("eucjp") # => "\x{A5EB}\x{A5D3}\x{A1BC}"
https://docs.python.org/ja/3/library/stdtypes.html#str.encode

encoding

【Python】該当なし

end_with?

【Ruby】r.end_with?("ン") # => false
【Python】r.endswith("ン") # => False
https://docs.python.org/ja/3/library/stdtypes.html#str.endswith

eql?

【Ruby】r.eql?(p) # => false
【Python】r == p # => False

force_encoding

【Python】該当なし

getbyte

【Ruby】r.getbyte(0) # => 227
【Python】r.encode("utf-8")[0] # => 227
https://docs.python.org/ja/3/library/stdtypes.html#str.encode

grapheme_clusters

【Python】該当なし

gsub

【Ruby】p.gsub(/イ|ン/){"ル"} # => "パルソル"
【Python】re.sub(r"イ|ン", "ル", p) # => "パルソル"

hash

【Ruby】r.hash # => 4357911679365031938
【Python】hash(r) # => 5233874843298921886
https://docs.python.org/ja/3/library/functions.html#hash

hex

【Ruby】"EFG".hex # => 239
【Python】int("EFG", 16) # => ValueError

include?

【Ruby】p.include?("パイ") # => true
【Python】"パイ" in p # => True

index

【Ruby】"rubypython".index("y") # => 3
【Python】"rubypython".find("y") # => 3
https://docs.python.org/ja/3/library/stdtypes.html#str.find

insert

【Python】該当なし

inspect

【Python】該当なし

intern

【Python】該当なし

length

【Ruby】r.length # => 3
【Python】len(r) # => 3
https://docs.python.org/ja/3/library/functions.html#len

lines

【Ruby】"ruby\npython\n".lines # => ["ruby\n", "python\n"]
【Python】"ruby\npython\n".splitlines() # => ["ruby", "python"]
https://docs.python.org/ja/3/library/stdtypes.html#str.splitlines

ljust

【Ruby】r.ljust(6, ":") # => "ルビー:::"
【Python】r.ljust(6, ":") # => "ルビー:::"
https://docs.python.org/ja/3/library/stdtypes.html#str.ljust

lstrip

【Ruby】"\ruby".lstrip # => "uby"
【Python】"\ruby".lstrip() # => "uby"
https://docs.python.org/ja/3/library/stdtypes.html#str.lstrip

match?

【Ruby】r.match?(/ビ/) # => true
【Python】re.search(r"ビ", r) != None # => True
https://docs.python.org/ja/3/library/re.html#re.search

match

【Ruby】p.match(/イ.ン/) # => #<MatchData "イソン">
【Python】re.search(r"イ.ン", p) # => <re.Match object; span=(1, 4), match="イソン">
https://docs.python.org/ja/3/library/re.html#re.search

new

【Ruby】String.new("ruby") # => "ruby"
【Python】str("ruby") # => "ruby"
https://docs.python.org/ja/3/library/stdtypes.html#str

next

【Ruby】p.next # => "パイソヴ"
【Python】p[:-1]+chr(ord(p[-1]) + 1) # => "パイソヴ"
https://docs.python.org/ja/3/library/functions.html#ord

oct

【Ruby】"10".oct # => 8
【Python】int("10", 8) # => 8
https://docs.python.org/ja/3/library/functions.html#int

ord

【Ruby】r.ord # => 12523
【Python】ord(r[0]) # => 12523
https://docs.python.org/ja/3/library/functions.html#ord

partition

【Ruby】"rubypython".partition("y") # => ["rub", "y", "python"]
【Python】"rubypython".partition("y") # => ("rub", "y", "python")
https://docs.python.org/ja/3/library/stdtypes.html#str.partition

prepend

【Python】該当なし

replace

【Python】該当なし

reverse

【Ruby】p.reverse # => "ンソイパ"
【Python】p[::-1] # => "ンソイパ"

rindex

【Ruby】"rubypython".rindex("y") # => 5
【Python】"rubypython".rfind("y") # => 5
https://docs.python.org/ja/3/library/stdtypes.html#str.rfind

rjust

【Ruby】r.rjust(6, ":") # => ":::ルビー"
【Python】r.rjust(6, ":") # => ":::ルビー"
https://docs.python.org/ja/3/library/stdtypes.html#str.rjust

rpartition

【Ruby】"rubypython".rpartition("y") # => ["rubyp", "y", "thon"]
【Python】"rubypython".rpartition("y") # => ("rubyp", "y", "thon")
https://docs.python.org/ja/3/library/stdtypes.html#str.rpartition

rstrip

【Ruby】"pytho\n".rstrip # => "pytho"
【Python】"pytho\n".rstrip() # => "pytho"
https://docs.python.org/ja/3/library/stdtypes.html#str.rstrip

scan

【Ruby】"rubypython".scan(/(.y)/) # => [["by"], ["py"]]
【Python】re.findall(r"(.y)", "rubypython") # => ["by", "py"]
https://docs.python.org/ja/3/library/re.html#re.findall

scrub

【Ruby】"\u30eb\u30d3\u30fc\xff".scrub # => "ルビー�"
【Python】b"\u30eb\u30d3\u30fc\xff".decode("utf-8","replace") # => \u30eb\u30d3\u30fc�
https://docs.python.org/ja/3/library/stdtypes.html#bytes.decode

setbyte

【Python】該当なし

size

【Ruby】r.size # => 3
【Python】len(r) # => 3
https://docs.python.org/ja/3/library/functions.html#len

slice

【Ruby】r.slice(1) == ?ビ # => true
【Python】r[1] == "ビ" # => True
https://docs.python.org/ja/3/reference/expressions.html#subscriptions

split

【Ruby】"ルビーとパイソン".split(/と/) # => ["ルビー", "パイソン"]
【Python】"ルビーとパイソン".split("と") # => ["ルビー", "パイソン"]
https://docs.python.org/ja/3/library/stdtypes.html#str.split

squeeze

【Python】該当なし

start_with?

【Ruby】p.start_with?("ル", "パ", "ン") # => true
【Python】p.startswith(("ル", "パ", "ン")) # => True
https://docs.python.org/ja/3/library/stdtypes.html#str.startswith

strip

【Ruby】"\ruby pytho\n".strip => "uby pytho"
【Python】"\ruby pytho\n".strip() # => "uby pytho"
https://docs.python.org/ja/3/library/stdtypes.html#str.strip

sub

【Ruby】"ruby python".sub(/y/){"ied"} # => "rubied python"
【Python】"ruby python".replace("y", "ied", 1) # => "rubied python"
https://docs.python.org/ja/3/library/stdtypes.html#str.replace

succ

【Ruby】"1.9.9".succ # => "2.0.0"
【Python】"1.9.9"[:-1]+str(int("1.9.9"[-1])+1) # => "1.9.10"
https://docs.python.org/ja/3/library/functions.html#int

sum

【Python】該当なし

swapcase

【Ruby】"Ruby Python".swapcase # => "rUBY pYTHON"
【Python】"Ruby Python".swapcase() # => "rUBY pYTHON"
https://docs.python.org/ja/3/library/stdtypes.html#str.swapcase

to_c

【Ruby】"12+3i".to_c # => (12+3i)
【Python】complex("12+3j") # =>(12+3j)
https://docs.python.org/ja/3/library/functions.html#complex

to_f

【Ruby】"2.7".to_f # => 2.7
【Python】float("2.7").to_f # => 2.7
https://docs.python.org/ja/3/library/functions.html#float

to_i

【Ruby】"3.8.2".to_i # => 3
【Python】int("3.8.2") # => ValueError
https://docs.python.org/ja/3/library/functions.html#int

to_r

【Ruby】"2/6".to_r # => (1/3)
【Python】fractions.fraction("2/6") # => Fraction(1, 3)
https://docs.python.org/ja/3/library/fractions.html#module-fractions

to_s

【Ruby】nil.to_s # => ""
【Python】str(None) # => "None"
https://docs.python.org/ja/3/library/functions.html#func-str

to_str

【Ruby】r.to_s # => "ルビー"
【Python】str(r) # => "ルビー"
https://docs.python.org/ja/3/library/functions.html#func-str

to_sym

【Python】該当なし

tr

【Ruby】"ruby python".tr("a-z", "A-Z") # => "RUBY PYTHON"
【Python】"ruby python".translate(str.maketrans(string.ascii_lowercase, string.ascii_uppercase)) # => "RUBY PYTHON"
https://docs.python.org/ja/3/library/stdtypes.html#str.translate

tr_s

【Python】該当なし

try_convert

【Ruby】String.try_convert(2.7) # => nil
【Python】str(3.8) # => "3.8"
https://docs.python.org/ja/3/library/functions.html#func-str

undump

【Python】該当なし

unicode_normalize

【Ruby】"#{r}と#{p}".unicode_normalize(:nfd).chars # => ["ル", "ヒ", "゙", "ー", "と", "ハ", "゚", "イ", "ソ", "ン"]
【Python】[c for c in unicodedata.normalize("NFD", f"{r}と{p}")] # => ["ル", "ヒ", '゙", "ー", "と", "ハ", '゚", "イ", "ソ", "ン"]
https://docs.python.org/ja/3/library/unicodedata.html#unicodedata.normalize

unicode_normalized?

【Ruby】"#{r}と#{p}".unicode_normalized?(:nfd) # => false
【Python】unicodedata.is_normalized("NFD", f"{r}と{p}") # => False
https://docs.python.org/ja/3/library/unicodedata.html#unicodedata.is_normalized

unpack1

【Python】該当なし

unpack

【Python】該当なし

upcase

【Ruby】"ruby python".upcase # => "RUBY PYTHON"
【Python】"ruby python".upper() # => "RUBY PYTHON"
https://docs.python.org/ja/3/library/stdtypes.html#str.upper

upto

【Ruby】"p".upto("r").to_a # => ["p", "q", "r"]
【Python】[chr(c) for c in range(ord("p"), ord("r")+1)] ["p", "q", "r"]
https://docs.python.org/ja/3/library/functions.html#chr

valid_encoding?

【Python】該当なし

補足

Ruby 2.7、Python 3.8 をベースにしてます。
https://docs.ruby-lang.org/ja/latest/class/String.html
https://docs.python.org/ja/3/library/stdtypes.html#textseq

  • !(破壊的変更)の対応は省略してます( Python の string クラスはイミュータブルの為)。
  • 【Python】該当なしできないを意味しません(数個の関数、メソッドの組み合わせでできる翻訳が思いつかないだけです)。
  • Ruby と Python では設計思想が違うので厳密に同じではありません。
  • Python の対応ドキュメントのリンクをできるだけ記載してますが見つからないのが(特に演算子)多々あります。
  • 標準ライブラリ・モジュールのみを前提にしてます。

あとがき

コーディングで ruby ではすぐ思いつくけど、python ではどう書く?という人(私)向けです。
網羅性を主眼としてます。そのため使ったことのない、使い道のわからないメソッドもそこそこありました。
同じアルゴリズム・ロジックを実現するのに、ruby はフルコース、python はア・ラ・カルト、が感想
多LGTMの為だけに、タイトルを『Rubyist が Python なんて大したことないね、とディスりつつこっそり見るチートシート』的なものにする誘惑に駆られた。(でも公式ガイドラインに準拠した)
他に配列クラス(Array→list)、辞書クラス(Hash→dict)のチートシートがあれば、とりあえずよいかな?(共同編集できればよいのだが)
右端にインデックスが全部掲示されない... (スクロールできたのか)

参考

Qiita記事検索
RubyistのためのPython入門
Python と Ruby の比較(環境・文法・リテラル編)
rubyとpythonの基本的なやつ対応表メモ
ruby でこう書くのは、python ならこう書く、のメモ

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?