2017年6月4日日曜日

置換する文字の数を指定する

文字列に含まれる特定の文字を置換する場合、replace メソッドを用いればよい。
なお、置換したい文字数を指定したい場合、replace メソッドの第3引数に当該値を指定すればよい。このとき、文字列の先頭から該当の数まで文字を置換することができる。
>> text = "Constitution of the United States of America"
>> print(text)
Constitution of the United States of America

>> text_1 = text.replace("o", "_")
>> print(text_1)
C_nstituti_n _f the United States _f America

>> text_2 = text.replace("o", "_", 2)
>> print(text_2)
C_nstituti_n of the United States of America

0 件のコメント:

コメントを投稿