非常に便利なjoinです。今まで使ったことがなかったのが不思議なぐらい
""の間に結合する文字を入れます。どういうことかというと例の通り
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ハイフン"-"で結合 | |
moji =["py","tho","n"] | |
print ("-".join(moji)) #=> py-tho-n | |
# なにもなしで結合 | |
moji =["py","tho","n"] | |
print ("".join(moji)) #=> python |
これでpython入門だったときに疑問だった文字列結合方法がクリアされました。
ちなみに入門のときはfor文とif文を使って頑張って結合していました
まわりくどすぎるわ!wwwwwww
日本語で説明すると
----------
文字列をインプットして、インプットした文字列をintにして、空のリストを作って、数字をリストに追加していき、ループで数字をprintして、最後の文字でなければカンマ","を書いて改行しないようにループ、最後の文字であればカンマを入れず改行もしない
----------
まぁたしかにできなくもないですが、いろいろ大変なことになってますね
入門の頃の試行錯誤でできた作品でした。
今なら同じようなものでも次のように書けます
まさかのワンライナーwww
11行のプログラムコードを1行にまとめることができました!
さて、久しぶりに勉強枠の記事でした。
問題解いていないとメソッド忘れちゃいますねぇ。定着するまでちゃんと復習しましょう
ちなみに入門のときはfor文とif文を使って頑張って結合していました
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = input() | |
nn = int(n) | |
a =[] | |
for i in range(nn): | |
a.append(input()) | |
for i in range(nn): | |
print(a[i],end="") | |
if i < nn-1: | |
print(",", end="") | |
elif i == nn-1: | |
print(".", end="") |
まわりくどすぎるわ!wwwwwww
日本語で説明すると
----------
文字列をインプットして、インプットした文字列をintにして、空のリストを作って、数字をリストに追加していき、ループで数字をprintして、最後の文字でなければカンマ","を書いて改行しないようにループ、最後の文字であればカンマを入れず改行もしない
----------
まぁたしかにできなくもないですが、いろいろ大変なことになってますね
入門の頃の試行錯誤でできた作品でした。
今なら同じようなものでも次のように書けます
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print ("-".join(list(map(int,input().split(" "))))) |
まさかのワンライナーwww
11行のプログラムコードを1行にまとめることができました!
さて、久しぶりに勉強枠の記事でした。
問題解いていないとメソッド忘れちゃいますねぇ。定着するまでちゃんと復習しましょう