非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

Pythonで文字列への挿入・置換

   

文字の挿入
text = 'Hello {}!'.format('World')
print(text)
#Hello World!

text = '{0} is a {1}.'.format('This', 'pen')
print(text);
#This is a pen.
 

 

文字列の置換(全置換)
text = 'pen pineapple apple pen.'
text = text.replace("apple","orange")
print(text);
#pen pineorange orange pen.
 

 

文字列の置換(1つ目のみ置換)
text = 'pen pineapple apple pen.'
text = text.replace("apple","orange", 1)
print(text);
#pen pineorange apple pen.
 

 

[ad#ad-1]

スポンサーリンク

 - Python