Python文字列を文字で増やす

Pythonの文字列につなげたい場合、文字列の結合記号( + )や文字列の繰り返し記号( * )を使って実装することができます。以下は代表的な方法です。

  1. +(プラス)演算子を利用する
string = "Hello"
new_string = string + " World"
print(new_string)  # 输出:Hello World
  1. 文字列のコピー演算子(*)を使用する:
string = "Hello"
new_string = string * 3
print(new_string)  # 输出:HelloHelloHello
  1. 文字列に文字を追加するには文字列のjoin()メソッドを使用する。
string = "Hello"
new_string = ''.join([string, ' World'])
print(new_string)  # 输出:Hello World

Python文字列は不変なため、文字操作を行うたびに新しい文字列が生成されます。

bannerAds