読者です 読者をやめる 読者になる 読者になる

亡きオウムに捧げる50の言葉

python

文字列定数

'Dead Parrot'

'''Dead Parrot'''

r'''Dead Parrot'''

"Dead Parrot"

"""Dead Parrot"""

r"""Dead Parrot"""

"Dead" " " "Parrot"

"Dead \
Parrot"

"Dead" \
" " \
"Parrot"

"\x44\x65\x61\x64\x20\x50\x61\x72\x72\x6f\x74"

"\104\145\141\144\40\120\141\162\162\157\164"

b'Dead Parrot'

br"Dead Parrot"

u"Dead Parrot"

ur"Dead Parrot"

u"\N{LATIN CAPITAL LETTER D}\N{LATIN SMALL LETTER E}"\
u"\N{LATIN SMALL LETTER A}\N{LATIN SMALL LETTER D}"\
u"\N{SPACE}\N{LATIN CAPITAL LETTER P}\N{LATIN SMALL LETTER A}"\
u"\N{LATIN SMALL LETTER R}\N{LATIN SMALL LETTER R}"\
u"\N{LATIN SMALL LETTER O}\N{LATIN SMALL LETTER T}"

u"\u0044\u0065\u0061\u0064\u0020\u0050\u0061\u0072\u0072\u006f\u0074"

u"\U00000044\U00000065\U00000061\U00000064\U00000020\U00000050\U00000061\U00000072\U00000072\U0000006f\U00000074"

演算子と文字列変換

"Dead" + " " + "Parrot"

"%s %s" % ("Dead", "Parrot")

"%(s1)s %(s2)s" % {"s1": "Dead", "s2":"Parrot"}

"%c%c%c%c%c%c%c%c%c%c%c" % (68, 101, 97, 100, 32, 80, 97, 114, 114, 111, 116)

"%X%x%x%x %c%c%c%c%c%c" % (13, 14, 10, 13, 80, 97, 114, 114, 111, 116)

"{0} {1}".format("Dead", "Parrot")

"{s1} {s2}".format(s1="Dead", s2="Parrot")

string.Template("$s1 $s2").substitute(s1="Dead", s2="Parrot")

スライス

"Dead Parrot"[:]

"Dead Parrot"[0:]

"-Dead Parrot-"[1:-1]

"D-e-a-d- -P-a-r-r-o-t-"[::2]

"torraP daeD"[::-1]

"-t-o-r-r-a-P- -d-a-e-D"[::-2]

join()

"".join(s for s in "Dead Parrot")

"".join(s for s in ("Dead Parrot",))

"".join(s for s in ("D","e","a","d"," ","P","a","r","r","o","t"))

"".join(s for s in ("Dead", " ", "Parrot"))

" ".join(s for s in ("Dead", "Parrot"))

"".join(s for s in ["Dead", "", " ", "", "Parrot"] if s <> "")

"".join(c for c in reversed("torraP daeD"))

文字列メソッド

"dEAD pARROT".swapcase()

"<<< Dead Parrot >>>".strip("<> ")

" ".join(s.capitalize() for s in ("dead", "parrot"))

"RGVhZCBQYXJyb3Q=".decode("BASE64")

'x\x9csIMLQ\x08H,*\xca/\x01\x00\x16\x87\x04\x07'.decode("zlib")

その他

eval("'Dead Parrot'")

struct.pack("bbbbbbbbbbb", 68, 101, 97, 100, 32, 80, 97, 114, 114, 111, 116)

unicodedata.normalize("NFKD", u"Dead Parrot")

operator.add("Dead ", "Parrot")

operator.mod("%s %s", ("Dead", "Parrot"))

(lambda x,y,z:x+y+z)("Dead", " ", "Parrot")