0
1

More than 3 years have passed since last update.

Pythonで文字列を配列にする、または配列を文字列にする方法

Last updated at Posted at 2020-07-12

Pythonで文字列を配列にする方法、また、配列を文字列にする方法を書いていきます。
よろしくおねがいします。

文字列を配列にする場合。

s = 'あいうえお'
print(list(s)) 
print([i for i in s])
#['あ', 'い', 'う', 'え', 'お'] と出力

配列を文字列にする場合。

print(''.join(['あ', 'い', 'う', 'え', 'お']))
print(*['あ', 'い', 'う', 'え', 'お'], sep='')
#あいうえお と出力

数字の配列を文字列にする場合。

n = [1, 2, 3, 4, 5]
print(''.join([str(i) for i in n]))
print(*[str(i) for i in n], sep='')
#12345と出力
#joinがintだとエラーが出るので、strに変換しています

以上です。
ありがとうございました。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1