# 5 [python3] Extract characters from a character string

Introduction

This article is written by a fledgling engineer who has been studying programming for about two months for the purpose of output. After biting ruby and js, I became interested in the trend python, so I started learning. This time I will write an article about character strings. This is a poor article, but I would appreciate it if you could point out any points that interest you! This article is based on the assumption that python3 and anaconda are installed on macOS.

1 index number

The string has an index number.

sample.py


sample = "abcdefghi"
         #↑↑↑↑↑↑↑↑↑       
         #012345678 When counting from before
       #-{987654321}When counting after

The index number of the character string counts the first character from 0. It is easy to make a mistake, so be careful. When counting from the back, start with -1.

2 Extract the character string with []

>>>sample = "abcdefghi"

Use [] to retrieve c with index number 2.

Interpreter


>>>sample = "abcdefghi"
>>>sample [2]
'c'

I was able to extract c from the string. Next, let's take out c using the index number when counting from the back.

Interpreter


>>>sample = "abcdefghi"
>>>sample [−7]
'c'

I was able to retrieve the string safely, but there is one caveat. It is Kano to extract a character string by index number, but it cannot be rewritten.

Interpreter


>>>sample = "abcdefghi"
>>>sample [−7] = "j"
  File "<stdin>", line 1
    sample [−7] = "j"
             ^
SyntaxError: invalid character in identifier

If you try to replace sample [-7] with "j" like this, you will get an error.

3 Call a substring

It is also possible to extract a character string by specifying a range using [].

Interpreter


>>>sample = "abcdefghi"
>>>sample[1:4]
   #String[Start number:End number]
'bcd'

In the above, index numbers 1 a to 4 d are selected and extracted. The start number and end number can be omitted.

Interpreter


>>>sample = "abcdefghi"
>>>sample[:4]
   #String[:End number]
'abcd'

In this case, the range from the beginning of the character string to the number 4 is specified.

Interpreter


>>>sample = "abcdefghi"
>>>sample[1:]
   #String[Start number:]
'bcdefghi'

In this case, you can specify the range from number 1 to the end.

At the end

This is the end of this article. Even if you know that the index number of a character string counts the first character from 0, it is easy to make a mistake.

Previous article → https://qiita.com/shin12032123/items/a8cc0d7612259683562e

Recommended Posts

# 5 [python3] Extract characters from a character string
Try to extract a character string from an image with Python3
[Python] How to invert a character string
Generate a class from a string in Python
How to extract the desired character string from a line 4 commands
How to quickly count the frequency of appearance of characters from a character string in Python?
Create a datetime object from a string in Python (Python 3.3)
Extract data from a web page with Python
Extract characters from images using docomo's character recognition API
[Python] Use a string sequence
[Python] How to expand variables in a character string
Python f character (formatted string)
[Python] Extract only numbers from lists and character strings
Python> Read from a multi-line string instead of a file> io.StringIO ()
Python learning basics ~ How to output (display) a character string? ~
Extract the value closest to a value from a Python list element
Call a Python function from p5.js.
Touch a Python object from Elixir
Is it a character string operation?
Django: Import a class from a string
Create a pandas Dataframe from a string.
Extract a page from a Wikipedia dump
python / Make a dict from a list.
Extract text from images in Python
Python-dynamically call a function from a string
[Beginner] Extract character strings with Python
Create a random string in Python
Extract strings from files in Python
Create an instance of a predefined class from a string in Python
[Introduction to Python] How to split a character string with the split function
[Introduction to Python] How to output a character string in a Print statement
How to get a string from a command line argument in python
Outputs a line containing the specified character string from a text file
[python] Extract text from pdf and read characters aloud with Open-Jtalk
Python string
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
I want to extract an arbitrary URL from the character string of the html source with python
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
I made a character counter with Python
Send a message from Python to Slack
[Python beginner memo] Python character string, path operation
Basic grammar of Python3 system (character string)
Create a deb file from a python package
Use Django from a local Python script
Manipulate BigQuery tables from a Python client
Python basic course (4 numeric type / character string type)
Call a command from Python (Windows version)
[Python] (Line) Extract values from graph images
A memorandum of python string deletion process
Fill the string with zeros in python and count some characters from the string
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
Tips: [Python] Randomly restore and extract an array from a fasta file
[Introduction to Python] How to write a character string with the format function
Tips: [Python] Extract only lines that do not contain a specific string
Use BeautifulSoup to extract a link containing a string from an HTML file
Send a message from Slack to a Python server
Python: String concatenation
Run a Python script from a C # GUI application
Edit Excel from Python to create a PivotTable
How to open a web browser from python
python string slice