I was coding in Python and there was a built-in method that I could use when manipulating strings, so I summarized it as my own memorandum. I don't know how to divide the system because it is organized by individual feeling.
The value in the character string can be described later using index. When used in combination with variables, it makes it easier to edit strings
python
print("This test is {0} for {1}.".format("easy", "you"))
# This test is easy for you.
When using variables
hoge = "easy"
fuga = "you"
print("This test is {0} for {1}.".format(hoge, fuga))
# This test is easy for you.
Add the previous value of the method between the arguments Cannot be added after the point starting with the argument and the last character of the argument
python
upper_hoge = "ABC"
lower_hoge = "abc"
print(lower_hoge.join(upper_hoge)) # AabcBabcC
Replace characters in a string The first argument is the character before replacement The second argument is the replaced character If you give a third argument, you can specify the number of characters to replace.
By setting a blank in the second argument, the target character can be deleted.
hoge = "ababab"
print(hoge.replace('a', 'A')) # AbAbAb
print(hoge.replace('a', 'A', 1)) # Ababab
print(hoge.replace('a', 'A', 2)) # AbAbab
print(hoge.replace('a', '')) # bbb
Remove the target string from the beginning and end The strings contained in the arguments are processed regardless of the order if the set matches. Remove the string if there are no arguments
python
hoge = " A testAtestAtest A "
#Leading and trailing blanks are removed
print(hoge.strip()) # A testAtestAtest A
#A is not removed because the beginning and end are blank
print(hoge.strip('A')) # A testAtestAtest A
fuga = "A testAtestAtest A"
#Leading and trailing A is removed
print(fuga.strip('A')) # testAtestAtest
#Looking from the beginning and the end, the argument'A','t',' 'The parts that do not correspond to any of
print(fuga.strip(("At "))) # estAtestAtes
Remove the target string from the end The strings contained in the arguments are processed regardless of the order if the set matches. Remove blanks if there are no arguments
python
hoge = " A testAtestAtest A "
#Only the leading blank is removed
print(hoge.lstrip()) # A testAtestAtest A
#Since the beginning is blank, nothing is removed because it does not reach the argument A
print(hoge.lstrip('A')) # A testAtestAtest A
fuga = "A testAtestAtest A"
#Only the leading A is removed
print(fuga.lstrip('A')) # testAtestAtest A
#Looking from the beginning, the argument'A','t',' 'The parts that do not correspond to any of
print(fuga.lstrip(("At "))) # estAtestAtest A
Remove the target string from the end The strings contained in the arguments are processed regardless of the order if the set matches. Remove blanks if there are no arguments
python
hoge = " A testAtestAtest A "
#Only trailing blanks are removed
print(hoge.rstrip()) # A testAtestAtest A
#Since the end is blank, nothing is removed because it does not reach the argument A.
print(hoge.rstrip('A')) # A testAtestAtest A
fuga = "A testAtestAtest A"
#Only the trailing A is removed
print(fuga.rstrip('A')) # A testAtestAtest
#Looking from the end, the argument'A','t',' 'The parts that do not correspond to any of
print(fuga.strip(("At "))) # A testAtestAtes
Counts from the beginning and returns where the corresponding character exists If the corresponding character does not exist, "-1" is output. It is also possible to pass the search start position and end position as arguments (Note that the index up to the previous index is searched for the value of the end position!)
python
hoge = "test"
print(hoge.find('t')) # 0
print(hoge.find('a')) # −1
print(hoge.find('t', 1, 4)) # 3
print(hoge.find('t', 1, 2)) # -1
Print the number of characters in the list
python
#This time output the number of t
hoge = "test"
print(hoge.count('t')) # 2
Decompose the string with the argument character and store it in the list The second argument is the number of splits
python
hoge = "a b c d e f g"
print(hoge.split(' ')) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(hoge.split(' ', 0)) # ['a b c d e f g']
print(hoge.split(' ', 1)) # ['a', 'b c d e f g']
print(hoge.split(' ', 4)) # ['a', 'b', 'c', 'd', 'e f g']
Splits the string like the split method, but splits from the right side when splitting The second argument is the number of splits
python
hoge = "a b c d e f g"
print(hoge.rsplit(' ')) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(hoge.rsplit(' ', 0)) # ['a b c d e f g']
print(hoge.rsplit(' ', 1)) # ['a b c d e f', 'g']
print(hoge.rsplit(' ', 4)) # ['a b c', 'd', 'e', 'f', 'g']
Method to determine the beginning of a string Returns true if it starts with an argument, false otherwise
python
hoge = "test"
print(hoge.startswith('te')) # True
print(hoge.startswith('tt')) # False
Method to determine the end of a string Returns true if it ends with an argument, false otherwise
python
hoge = "test"
print(hoge.endswith('st')) # True
print(hoge.endswith('tt')) # False
Convert strings to uppercase
python
lower_hoge = "abc"
print(lower_hoge.upper()) # ABC
Convert strings to lowercase
python
upper_hoge = "ABC"
print(upper_hoge.lower()) # abc
Swap uppercase and lowercase
python
hoge = "AbcDefG"
print(hoge.swapcase()) # aBCdEFg
Uppercase the first character
python
hoge = "test"
print(hoge.capitalize()) # Test
· Python 3.7.5 documentation https://docs.python.org/ja/3.7/library/stdtypes.html#string-methods
Recommended Posts