Learning notes, memorandums
Created as an instance of the str
class. There is no distinction between characters and strings.
Basic methods of string type
a = ' Hello, World \n'
b = a.strip () # Remove whitespace before and after
c = b.lower () # Convert to lowercase
d = b.upper () # Convert to uppercase
e = b.swapcase () # Invert case
f = b.title () # Convert only the first letter of a word to uppercase
g = b.split ('') # Split into list by specifying delimiter
h = b.splitlines () # Split into a list separated by line breaks
Be careful when the loop causes multiple additional string operations, such as when you want to read text data from a file and save that data as a str
instance.
text = ''
with open('test.txt') as f:
temp = f.readline()
while temp:
text += temp
temp = f.readline()
In this description method, for each loop, one line of memory is allocated, the memory used up to the previous one and the memory newly read are newly allocated, and the old memory used up to the previous one is released. Since the processing is performed, the memory efficiency is very low and the processing speed is also slow.
list = []
with open('test.txt') as f:
temp = f.readline()
while temp:
list.append(temp)
temp = f.readline()
text = ``.join(list)
To avoid compromising memory efficiency, you can save it as a list and combine it later.
By using the join
method, you can join the contents of the list by specifying the delimiter character.
startswith ()
: Whether the string starts with the string specified by the argument
ʻEndswith () `: Whether the string ends with the string specified by the argument
find ()
: Get the index of the character string specified by the argument, -1 if not found
ʻIndex (): Get the index of the character string specified by the argument,
ValueError if not found You can also use the ʻin
operator to determine if it is included.
count ()
: Count the number of times the character string specified by the argument appears.
replace ()
: Replaces the character string specified by the first argument with the character string specified by the second argument, even if there are multiple target character strings, all are replaced.
re
packagere.match ()
: Returns the match ()
class if the beginning of the string matches the string specified by the argument, and None
if it does not match.
You can get the matched location with re.match (). span ()
.re.search ()
: Returns the match ()
class if it matches the string specified by the argument, and None
if it does not match.
You can get the matched location with re.search (). span ()
.compile ()
method.findall ()
: Get all matching strings and save as a list.+
Represents one or more repetitions, and *
represents zero or more repetitions.
By prefixing the string with f
or F
, the part enclosed by {}
becomes a field that is replaced by an external variable or calculation formula.
a = 'World'
b = f'Hello, {a}'
print(b)
c = 10
d = 20
e = F'{c} + {d} = {c+d}'
print(e)
Execution result
Hello, World 10 + 20 = 30
The format ()
method replaces the index or variable in the {}
field with an argument.
a = '{0} is {1}'
b = a.format('apple', 'red')
print(b)
c = '{fruits} is {color}'
d = c.format(fruits = 'melon', color = 'green')
print(d)
Execution result
apple is red melon is green
Align the character string by filling it with the character string specified after :
.
a = 'Title'
b = '{:<10}'.format(a) # 'Title '
c = '{:>10}'.format(a) # ' Title'
d = '{:-^10}'.format(a) # '--Title---'
Recommended Posts