0
0

文字の検索 Python3編

Posted at

文字列 S と文字 c が与えられるので、 c は S の何文字目に現れるかを調べてください。

何もみないで書いたやつ

S = input()
c = input()
order = 1
for i in S:
    if i == c:
       print(order)
       break
    else:
        order += 1

答えはenumerate()という関数を用いていました
これを用いると、配列の要素だけでなくインデックスも同時に取得可能
下記の様に書きます i, eleは変数名で変更可能。

s = input()
c = input()

for i, ele in enumerate(s):
   if ele == c:
       print(i + 1)

0
0
1

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
0