Tutorial on the use of str (string) in python3
- 2020-05-27 06:16:44
- OfStack
This article mainly introduces the operation summary of python3 to str (string). It is very detailed in this article. Let's have a look at it if you need it.
S 5en__ function (append the string later)
s1 ='Hello'
s2 = s1.__add__(' boy!')
print(s2)
# Output: Hello boy!
S. S. S. S. S. S. S. S. S. S. S. S.
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
S. S. S. S. S. S. S. S. S. S. S. S.
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
# Output: False
__format__
# placeholder
__getattribute__
# placeholder
__getitem__
# placeholder
__getnewargs__
# placeholder
S = s = s = s = s = s
print('b'.__ge__('a'))
# Output: True
__gt__ (greater than)
print('b'.__ge__('a'))
# Output: True
__hash__
# placeholder
__iter__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
0
S 51en__ (return string length)
print('abc'.__len__())
# Output: 3
S 55en__ (less than or equal to)
print('b'.__le__('a'))
# Output: False
S 59en__ (less than)
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
3
__mod__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
4
__mul__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
5
__new__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
6
__ne__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
7
__repr__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
8
__rmod__
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
# Output: True
9
__rmul__
# placeholder
__sizeof__
# placeholder
S 95en__ (return to self)
print('abc'.__str__())
# Output: abc
capitalize(capital letters)
s = 'tom'
print(s.capitalize())
# Output: Tom
casefold (uppercase to lowercase)
s = 'TOM'
print(s.casefold())
# Output: tom
center (specify length and padding characters, center the content, and space for padding characters)
s = 'Tom'
print(s.center(20,'-'))
# Output: --------Tom---------
count(count the number of occurrences of a string, parameter 2: start position, parameter 3: end position)
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
# Output: False
6
encode (encoding)
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
# Output: False
7
endswith (second argument to determine whether a string ends with a character or string: start position, third argument: end position)
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
# Output: False
8
expandtabs (convert 1 tab key to 7 Spaces)
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
# Output: False
9
find (find the index position of a character or string, parameter 2: start position, parameter 3: end position)
s = 'Hello'
print(s.find('o'))
print(s.find('o',0,3)) # Unable to return -1
# Output: 4
# -1
format(string formatting/concatenation)
# placeholder
1
format_map
# placeholder
2
index (looking for the index position of a character or string, like find, if the character does not exist, an error is reported)
# placeholder
3
isalnum(whether letters or Numbers)
# placeholder
4
isalpha(whether letter or not)
# placeholder
5
isdecimal (whether it is a decimal number)
# placeholder
6
isdigit (whether it is a number)
# placeholder
7
isidentifier (whether identifier/variable name)
# placeholder
8
islower (all lowercase)
s = 'Hello'
print(s.islower())
# Output: False
isnumeric (whether it is a number)
# placeholder
0
isprintable(printable characters or not)
# placeholder
1
isspace (whether space or not)
# placeholder
2
istitle (whether to capitalize the title/the first letter of each word)
# placeholder
3
isupper (all uppercase)
# placeholder
4
join(concatenates the elements of a sequence to generate a new string with the specified characters)
# placeholder
5
ljust(specify length and fill characters, content left aligned, fill characters left blank as Spaces)
# placeholder
6
lower (all strings in lowercase)
# placeholder
7
lstrip(removes the character specified to the left of the string, defaults to space)
# placeholder
8
maketrans(create a conversion table for character mapping, used in conjunction with the translate function)
# placeholder
9
partition (specify the separator to split the string)
# placeholder
0
replace(replace old (the old string) in the string with new(the new string), and if the third parameter max is specified, replace it no more than max times.)
# placeholder
1
rfind(looks for the location where the specified string appears from the right, and returns -1 if there is no match)
# placeholder
2
rindex(find the location where the specified string appears from the right, and report an error if there is no match)
# placeholder
3
rjust(specifies length and padding characters, right-aligned content, padding characters left blank as Spaces)
# placeholder
4
rpartition (specify the delimiter to split the string from the right)
# placeholder
5
rsplit(specify the separator to slice the string, if the second parameter num is specified, num is separated only once, and 1 list is returned)
# placeholder
6
rstrip(removes the specified character at the end of the string, defaults to space)
s = '!!! I am Tom !!!'
print(s.rstrip('!'))
# Output: !!! I am Tom
split(specify the delimiter to slice the string, if the second parameter num is specified, num is separated only once, and 1 list is returned)
s = 'a b c d'
print(s.split())
print(s.split(' ',2)) # Start on the left and space it twice
# Output: ['a', 'b', 'c', 'd']
# ['a', 'b', 'c d']
splitlines(returns 1 list, delimited by newline)
# placeholder
9
startswith (second argument to determine whether a string begins with a character or string: start position, third argument: end position)
# placeholder
0
strip(specified characters before and after deletion of string, default to space)
# placeholder
1
swapcase (case interchange)
# placeholder
2
title(converted to title, which is the first letter of each word in uppercase)
# placeholder
3
translate (character substitution according to the table created by the maketrans method)
# placeholder
4
upper(lowercase to uppercase)
# placeholder
5
zfill(specifies the length of the string. Align the original string to the right, fill in 0 in front)
s = 'Hello'
print(s.zfill(10))
# Output: 00000Hello
conclusion