1
0

More than 1 year has passed since last update.

pythonで"/"を含む文字列を"/"で区切って一番最後の要素を出力する際の備忘録

Posted at

pythonで"/"を含む文字列を"/"で区切って一番最後の要素を出力する際の備忘録

test.py
# /を含む文字列
s = "xxx/yyy/zzz"

# "/"区切りで分割してリスト化
s = s.split("/")

# 最後の要素を取得
s = s[-1]

# 出力
print(s)
test2.py
# 1行で実行する場合は以下の通り
s = "xxx/yyy/zzz"
print(s.split("/")[-1])
$ python test.py
zzz
$ python test2.py
zzz

他にも色々な方法がありそうな気がする

参考

1
0
0

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
1
0