「逆引きPython/文字列」の編集履歴(バックアップ)一覧に戻る
>>> s = "こんにちは世界" >>> print(s) こんにちは世界 >>> bs = s.encode() #引数を省略(UTF_8でエンコード) >>> print(bs) b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c' >>> print(bs.decode()) こんにちは世界
>>> s = "こんにちは世界" >>> bs = s.encode("cp932") #cp932でエンコード >>> print(bs) b'\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd\x90\xa2\x8aE' >>> print(bs.decode("cp932")) こんにちは世界
>>> s = "Hello" >>> s + " World!" 'Hello World!'
>>> s = "Hey " >>> s * 3 'Hey Hey Hey '
>>> s = "I love SPAM" >>> s.upper() 'I LOVE SPAM' >>> s.lower() 'i love spam'
>>> s = "I love SPAM" >>> s.swapcase() 'i LOVE spam'
>>> s = """ ... SPAM ... Egg ... Bacon ... """ >>> print(s) SPAM Egg Bacon >>>
>>> s = "SPAM Egg Bacon" >>> s[3] 'M' >>> s[0:3] 'SPA' >>> s[0:4] 'SPAM' >>> s[2:4] 'AM' >>> s[:4] 'SPAM' >>> s[4:] ' Egg Bacon' >>> s[-5:] 'Bacon' >>> s[:-6] 'SPAM Egg' >>> s[1:11:2] 'PMEgB'
>>> for x in "SPAM": ... print(x) ... S P A M
>>> s = " SPAM " >>> s.strip() 'SPAM' >>> s.lstrip() 'SPAM ' >>> s.rstrip() ' SPAM'
>>> s = "SPAM Egg Bacon" >>> len(s) 14
>>> i = 1 >>> s = "123" >>> i + int(s) 124
>>> s = "123" >>> float(s) 123.0
>>> chr(97) 'a'
>>> s = "SPAM" >>> s.center(12) ' SPAM ' >>> s.ljust(12) 'SPAM ' >>> s.rjust(12) ' SPAM'
>>> s = "abc abc abc abc" >>> s.replace("ab","xy") 'xyc xyc xyc xyc' >>> s.replace("ab","uv",2) 'uvc uvc abc abc'
>>> s = "SPAM Egg SPAM Bacon" >>> s.find("SPAM") 0 >>> s.find("Egg") 5 >>> s.find("SPAM",4) 9 >>> s.find("Sausage") -1 >>> s.index("Egg") 5 >>> s.rindex("SPAM") 9 >>> s.rfind("SPAM",0,9) 0
>>> s1 = "SPAM Egg Bacon" >>> s1.split() ['SPAM', 'Egg', 'Bacon'] >>> s2 = "1,2,3" >>> s2.split(",") ['1', '2', '3'] >>> s3 = "4 :::: 5 :::: 6" >>> s3.split(" :::: ") ['4', '5', '6']