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.

【初心者】Phthonで文字列を抽出する

Posted at

######文字列

######0以上のインデックスを指定 一番左の文字を指定したいときは[0]。
>>> w = "abcde"
>>> w[0]
'a'
>>> w[4]
'e'

######マイナスのインデックスを指定
一番右の文字を指定したいときは[-1]。

>>> w = "abcde"
>>> w[-5]
'a'
>>> w[-1]
'e'

######インデックスの範囲外を指定するとエラー

>>> w = "abcde"
>>> w[5]
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    word[5]
IndexError: string index out of range
>>> w[-6]
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    w[-6]
IndexError: string index out of range

######コロンを利用して範囲を指定する

>>> w = "abcde"
>>> w[1:4]
'bcd'
>>> w[0:3]
'abc'
>>> w[:3]
'abc'
>>> w[3:0]
''
>>> w[-1:]
'e'
>>> w[:-1]
'abcd'
>>> word[-4:-2]
'bc'
>>> word[-2:-4]
''
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?