[Beginner] Extract character strings with Python

String
###### Specify an index of 0 or more [0] to specify the leftmost character.
>>> w = "abcde"
>>> w[0]
'a'
>>> w[4]
'e'
Specify a negative index

If you want to specify the rightmost character, use [-1].

>>> w = "abcde"
>>> w[-5]
'a'
>>> w[-1]
'e'
Error if you specify outside the index range
>>> 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
Use a colon to specify a 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]
''

Recommended Posts

[Beginner] Extract character strings with Python
3-3, Python strings and character codes
Web scraping beginner with python
[Python] Extract only numbers from lists and character strings
[Python3] Be careful with removing character strings (strip, lstrip, rstrip)
Extract the xz file with python
[Automation] Extract Outlook appointments with Python
Let's play with Excel with Python [Beginner]
Extract strings from files in Python
Try to extract a character string from an image with Python3
I made a character counter with Python
Convert character strings to features with RoBERTa
[Python beginner memo] Python character string, path operation
[Introduction to Udemy Python3 + Application] 11. Character strings
Beginner ABC154 (Python)
Beginner ABC156 (Python)
FizzBuzz with Python3
Scraping with Python
python beginner memo (9.2-10)
Statistics with python
Scraping with Python
Python with Go
[Beginner] Python array
Twilio with Python
Integrate with Python
Play with 2016-Python
python character code
AES256 with python
Tested with Python
Beginner ABC155 (Python)
python starts with ()
with syntax (Python)
[Beginner] Python functions
Bingo with python
Beginner ABC157 (Python)
Zundokokiyoshi with python
PyQ ~ Python Beginner ~
Python beginner memo (2)
Excel with Python
Python beginner Zundokokiyoshi
Microcomputer with Python
Cast with python
[Automation] Extract the table in PDF with Python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Character encoding when dealing with files in Python 3
Links and memos of Python character code strings
Extract zip with Python (Japanese file name support)
Extract email attachments received in Thunderbird with Python
Convert the character code of the file with Python3
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Solve "AtCoder version! Ant book (beginner)" with Python!
Convert strings to character-by-character list format with python
Extract data from a web page with Python
Character strings placed in GCS with python are garbled when viewed with a browser
Serial communication with Python
[Python] Summary of eval / exec functions + How to write character strings with line breaks
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python