0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python 文字列のスライス

Posted at

#はじめに
この記事を書く理由は、競プロを始めてスライスを使う場面が多く、頭の中が混雑してきたので簡単にまとめようと思ったことがきっかけです。単なるスライスのメモです。

##プログラムコード
数字の文字列です。数字の文字列にした理由は、出力結果がわかりやすいと思ったからです。


s = "12345"
print(s[-1])  # 末尾
5 # 出力結果
print(s[:-1])  # 末尾を削除
1234
print(s[0])  # 先頭
1
print(s[0:])  # 先頭から最後まで
12345
print(s[1:])  # 1番目から最後まで
2345

start = "6" + s[1:]
print(start) # 先頭を削除して、6を先頭に追加
62345

end = s[:-1] + "6"
print(end) # 末尾を削除して、6を末尾に追加
12346
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?