Reference site: [Introduction to Python] Thorough explanation of the character string type used in Python!
Python handles various data types. Of these, the string type is most often used. Compared to other languages, Python is easier to work with strings and has many useful methods.
This time, I will explain the basics of string types in Python.
To represent a string in Python, enclose it in either single quotation marks (') or double quotation marks ("). It doesn't matter which one you use, but be sure to unify it.'And" It becomes confusing when used in a mixed manner.
print('Hello, World')
print("Hello, World")
Execution result
Hello, World Hello, World
A character string or a variable that stores a character string can be concatenated with the "+" operator and repeated with the "*" operator.
str1 = 'Python'
str2 = 'Programming'
print(str1 + ' ' + str2)
print(str1 * 3)
Execution result
Python Programming PythonPythonPython
Python strings can be indexed, and the index can be used to retrieve one of the strings. Note that the index starts at 0 instead of 1. If a negative number is specified, the numbers will be counted in order from the right. Also note that negative indexes start with -1 instead of 0.
str1 = 'Python'
print(str1[3]) #Extract the third character
print(str1[-3]) #Extract the third character from the right
Execution result
h h
Python also supports slicing operations. By using slices, you can extract only the character strings in the specified range. The method of slicing is as follows.
Note that the "index to start slicing" character is included in the slice result, but not the "index to end slicing" character. In other words, it is fetched up to the "index that ends the slice – 1".
str1 = 'Python'
print(str1[2:4]) #Take out from 2nd to 3rd
print(str1[1:5]) #Take out from 1st to 4th
Execution result
th ytho
In addition, the index that starts slicing and the index that ends can be omitted. If the index that starts is omitted, 0 is specified, and if the index that ends is omitted, the size of the character string (= up to the last character) Will be specified.
str1 = 'Python'
print(str1[:5]) #Take out from 0th to 4th
print(str1[1:]) #Take out from first to last
Execution result
Pytho ython
format
format is used to specify the format of a string or variable and embed it in another string. For example, it is used when printing an arbitrary character string and variable together.
'Arbitrary string{0}{1}…'.format(Variable 1,Variable 2 ....)
In any string, put {} where you want to embed the string or variable. Then, the argument of the format method is embedded in the {}. If you want to embed multiple strings or variables, put {} in the string along with the subscripts such as {0}, {1} and pass it to format as multiple arguments.
a = 1
b = 2
print('{0} + {1} = {2}'.format(a, b, a + b))
Execution result
1 + 2 = 3
index
index checks if a string contains the specified character or string, and if so, returns the first index in which the character or string was found.
String.index(探したいString[Starting position,End position])
You can use slices to specify where to start and where to end the search. Both can be omitted, and if both are omitted, the entire character string will be searched.
str1 = 'python programming'
print(str1.index('th'[1:10]))
Execution result
3
split
split is a method that splits a string by any string. The divided character string becomes a list type.
list= 'String'.split(区切りString,Maximum number of divisions)
The maximum number of divisions can be omitted, and if omitted, the character string will be divided as much as possible.
str1 = 'python, programming, start'
print(str1.split(','))
Execution result
[‘python’, ‘ programming’, ‘ start’]
Recommended Posts