pythonのstr/string文字列パラメータ付き方法の詳細(Python第3編)
Stringメソッドコードフラグメント
本文を見て見られない感じがしたら、文章のフォーマットのアドバイスをコメントで教えてください.
実行結果
コメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよ
本文を見て見られない感じがしたら、文章のフォーマットのアドバイスをコメントで教えてください.
str1 = 'I am 23 years old! 23 '
#
print("str1 :"+str(len(str1)))
# , N , 。
print("center+"+str1.center(50,'*'))
print("center+"+str1.center(50))
# , ,
print("count+"+str(str1.count('a')))
print("count+"+str(str1.count('a',0,3)))
# ,
# GBK/UTF-8/GB2312 b bytes
# , 。
print("encode+"+str(str1.encode('GB2312','ERROR')))
# print(str.decode())
# decode ,python3
# ,
# start,end position
# 0 ,
print("endswith+"+str(str1.endswith('o',0,15)))
print("endswith+"+str(str1.endswith((' '))))
# tab (‘\t’) , 8
str_1 = 'abcd\tefg\thijklmn\topqrstuvw\txyz'
print("expandtabs+"+str_1.expandtabs())
print("expandtabs+"+str_1.expandtabs(12))
# str.find(str, beg = 0, end = len(string))
# -1
#
print("find+"+str(str1.find('a')))
print("find+"+str(str1.find('a',3)))
print("find+"+str(str1.find('a', 3,8)))
# str.format 。
Person= {
'name': 'Yorlen', 'age': 23}
print("format_map+"+'My name is {name},I am {age} years old!'.format_map(Person))
#
# The beginning and end can be omitted
print("index+"+str(str1.index('am', 0, len(str1))))
am = 'am'
print("index+"+str(str1.index(am,0,len(str1))))
#
seq = ('1', '2', '3', '4')
print("join+"+"_".join(seq))
# ,
# 。
#
print("ljust+"+str1.ljust(30 , ' ')+"end")
# ,
str2 = '1111111333333313131322222221111111'
print("lstrip+"+str2.lstrip('1'))
print("lstrip+"+str2.lstrip('13'))
# ,
# unicode
print('maketrans'+str(str.maketrans('aeiou','12345')))
print("maketrans+"+str(str.maketrans('aeiou',' ')))
print("maketrans+"+str(str.maketrans('aeiou', '!@#¥%')))
print(' unicode “ ” :'+str(ord(' ')))
# , ,
print("partition+"+str(str1.partition(' ')))
# ?
#
print("partition+"+str(str1.partition('I')))
# partition
print("partition+"+str(str1.partition(' ')))
#
# Python3.9
#print(str1.removeprefix('I'))
#
# Python3.9
# print(str1.removesuffix())
# old new, num, num
print("replace+"+str2.replace('1', '5'))
print("replace+"+str2.replace('1', '5', 3))
# , -1
print("rfind+"+str(str1.rfind('3')))
print("rfind+"+str(str1.rfind('5')))
# ,
# substring not found
print("rindex+"+str(str1.rindex('23')))
#print(str1.rindex('234'))
# ,
# 。
#
print("rjust+"+str1.rjust(36,'a'))
# , ,
print("rpartition+"+str(str1.rpartition(' ')))
print("rpartition+"+str(str1.rpartition('p')))
# , num , num+1
print("rsplit+"+str(str1.rsplit()))
# ,
print("rsplit+"+str(str2.rstrip('1')))
# , num , num+1
print("split+"+str(str1.split(' ')))
print("split+"+str(str1.split(' ',1)))
# ('\r','
','\r
') , , False True
# False ('\r','
','\r
')
# True ('\r','
','\r
'),
str3 = 'abc
de\r
\r
f
\rg\r\rhijk\rlm
n'
print("splitlines+"+str(str3.splitlines()))
print("splitlines+"+str(str3.splitlines(True)))
#
# , True
# False
print("startswith+"+str(str1.startswith('I')))
print("startswith+"+str(str1.startswith('D')))
# string Lstrip() Rstrip()
print("strip+"+str2.strip('12'))
# table , , del
print("translate+"+str1.translate({
97: 49, 101: 50, 105: 51, 111: 52, 117: 53}))
# translate maketrans , 。
print("translate+"+str1.translate(str.maketrans('aeiou','12345')))
# width , string , 0
# z zero
print("zfill+"+str1.zfill(30))
実行結果
str1 :25
center+************I am 23 years old! 23 *************
center+ I am 23 years old! 23
count+2
count+1
encode+b'I am 23 years old!\xce\xd2\xbd\xf1\xc4\xea23\xcb\xea\xc1\xcb'
endswith+True
endswith+True
expandtabs+abcd efg hijklmn opqrstuvw xyz
expandtabs+abcd efg hijklmn opqrstuvw xyz
find+2
find+10
find+-1
format_map+My name is Yorlen,I am 23 years old!
index+2
index+2
join+1_2_3_4
ljust+I am 23 years old! 23 end
lstrip+333333313131322222221111111
lstrip+22222221111111
maketrans{97: 49, 101: 50, 105: 51, 111: 52, 117: 53}
maketrans+{97: 20320, 101: 26159, 105: 25105, 111: 23453, 117: 36125}
maketrans+{97: 65281, 101: 64, 105: 35, 111: 65509, 117: 37}
unicode “ ” :23453
partition+('I', ' ', 'am 23 years old! 23 ')
partition+('', 'I', ' am 23 years old! 23 ')
partition+('I am 23 years old! 23 ', ' ', '')
replace+5555555333333353535322222225555555
replace+5551111333333313131322222221111111
rfind+22
rfind+-1
rindex+21
rjust+aaaaaaaaaaaI am 23 years old! 23
rpartition+('I am 23 years', ' ', 'old! 23 ')
rpartition+('', '', 'I am 23 years old! 23 ')
rsplit+['I', 'am', '23', 'years', 'old! 23 ']
rsplit+111111133333331313132222222
split+['I', 'am', '23', 'years', 'old! 23 ']
split+['I', 'am 23 years old! 23 ']
splitlines+['abc', '', 'de', '', 'f', '', 'g', '', 'hijk', 'lm', 'n']
splitlines+['abc
', '
', 'de\r
', '\r
', 'f
', '\r', 'g\r', '\r', 'hijk\r', 'lm
', 'n']
startswith+True
startswith+False
strip+3333333131313
translate+I 1m 23 y21rs 4ld! 23
translate+I 1m 23 y21rs 4ld! 23
zfill+00000I am 23 years old! 23
Process finished with exit code 0
コメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよコメントコレクションを転送するよ