Home > Backend Development > Python Tutorial > Detailed explanation of the functions of python3 strings with examples

Detailed explanation of the functions of python3 strings with examples

高洛峰
Release: 2017-03-19 13:57:11
Original
1468 people have browsed it

addFunction (Append string at the end)

s1 ='Hello'
s2 = s1.__add__(' boy!')
print(s2)

#输出:Hello boy!
Copy after login

contains (determine whether a string is contained, return True if it does)

s1 = 'Hello'
result = s1.__contains__('He')
print(result)

#输出:True
Copy after login

eq (Judge whether two strings are the same, return True if they are the same)

s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)

#输出:False
Copy after login

format

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

getattribute

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

getitem

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

getnewargs

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

ge (greater than or equal to)

print('b'.ge('a'))#输出:True
Copy after login
Copy after login

gt(greater than)

print('b'.ge('a'))#输出:True
Copy after login
Copy after login

hash

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

iter

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

len(return string length)

print('abc'.len())#输出:3
Copy after login

le(less than or equal to)

print('b'.le('a'))#输出:False
Copy after login

lt(less than)

print('b'.lt('a'))#输出:False
Copy after login

mod

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

mul

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

new

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

ne

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

repr

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

rmod

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

rmul

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

sizeof

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

str(return to self)

print('abc'.__str__())
#输出:abc
Copy after login

capitalize(capitalize the first letter)

s = 
'tom'print(s.capitalize())
#输出:Tom
Copy after login

casefold(convert uppercase to lowercase)

s = 
'TOM'print(s.casefold())#
输出:tom
Copy after login

center(specify length and padding characters , the content is centered, and the fill character is left blank if it is a space)

s =
 'Tom'print(s.center(20,'-'))
 #输出:--------Tom---------
Copy after login

count(Calculate the number of occurrences of a certain string, the second parameter: starting position, the third parameter : End position)

s = 
'aabbbcccccdd'print(s.count('cc',3,11))
#输出:2
Copy after login

encode (encoding)

s = 
"中文"print(s.encode('gbk'))
#输出:b'\xd6\xd0\xce\xc4'
Copy after login

endswith (to determine whether the string ends with a certain character or string, the second parameter: starting position, the third Parameters: end position)

s = 
'Projects'print(s.endswith('ts'))print(s.endswith('e',0,5))
#输出:True#     True
Copy after login

expandtabs (convert 1 tab key into 7 spaces)

s = 
'H\ti'print(s.expandtabs())
#输出:H       i
Copy after login

find (find the index position of a character or string, second parameter: Starting position, third parameter: end position)

s = 
'Hello'print(s.find('o'))print(s.find('o',0,3))  
#找不到返回-1#输出:4#     -1
Copy after login

format (string formatting/splicing)

name = 
'Tom'age = 18s = '{0}\'s age is {1}'.format(name,age)print(s)
#或者str = '{name}\'s age is {age}'result = str.format(age=18,name='Tom')print(result)
#输出:Tom's age is 18
Copy after login

format_map

#占位
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

index (Find the index position of a character or string, which is different from find. If the character does not exist, an error will be reported)

s = 'Hello'print(s.index('o'))print(s.index('e',0,3))
#输出:4#     1
Copy after login

isalnum (whether it is a letter or number)

s = '!#'print(s.isalnum())
#输出:False
Copy after login

isalpha (whether it is a letter)

s = '123'print(s.isalpha())
#输出:False
Copy after login

isdecimal (whether it is a decimal number)

s = '123'print(s.isdecimal())
#输出:True#True: Unicode数字,,全角数字(双字节)#False: 罗马数字,汉字数字#Error: byte数字(单字节)
Copy after login

isdigit (whether it is a number)

s = '123'print(s.isdigit())
#输出:True#True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字#False: 汉字数字
Copy after login

isidentifier (whether it is an identifier Symbol/Variable name)

s = '1num'print(s.isidentifier())
#输出:False#因为变量名不能以数字开头
Copy after login

islower (whether all lowercase letters)

s = 'Hello'print(s.islower())
#输出:False
Copy after login

isnumeric (whether it is a number)

s = '123'print(s.isnumeric())
#输出:True#True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
Copy after login

isprintable (whether it is Printable characters/can it be output as is)

s = '\n'print(s.isprintable())
#输出:False
Copy after login

isspace (whether it is a space)

print(' '.isspace())print('\t'.isspace())
#输出:True#     True
Copy after login

istitle (whether it is a title/the first letter of each word is capitalized)

print('Hello Boy'.istitle())print('hello boy'.istitle())
#输出:True#     False
Copy after login

isupper (Whether all are uppercase letters)

print('BOY'.isupper())print('Boy'.isupper())
#输出:True#     False
Copy after login

join (join the elements in the sequence with the specified characters to generate a new string)

s = ['H','e','l','l','o']print(''.join(s))print('-'.join(s))
#输出:Hello#     H-e-l-l-o
Copy after login

ljust (specify the length and padding characters, the content is left Alignment, if the padding character is left blank, it will be a space)

s = 'Hello'print(s.ljust(10,'-'))
#输出:Hello-----
Copy after login

lower (all strings are changed to lowercase)

s = 'TOM'print(s.lower())
#输出:tom
Copy after login

lstrip (remove the specified characters on the left side of the string, the default is a space)

s = '   Tom'print(s.lstrip())
#输出:Tom
Copy after login

maketrans (Create a conversion table for character mapping, used with the translate function)

intab = "abcde"outtab = "12345"trantab = str.maketrans(intab, outtab)
str = "Hello abc"print (str.translate(trantab))
#输出:H5llo 123
Copy after login
Copy after login

partition (Specify the separator to split the string)

s = 'IamTom'print(s.partition('am'))
#输出:('I', 'am', 'Tom')
Copy after login

replace (Replace the characters The old (old string) in the string is replaced with new (new string). If the third parameter max is specified, the replacement will not exceed max times. )

s = 'Tom'print(s.replace('m','o'))
#输出:Too
Copy after login

rfind(Find the position where the specified string appears from the right, if there is no match, return -1)

s = 'one two one'print(s.rfind('one'))print(s.rfind('one',0,6))  
#指定起始和结束位置#输出:8#     0
Copy after login

rindex(Find the position where the specified string appears from the right, if there is no match If the item matches, an error will be reported)

s = 'one two one'print(s.rindex('one'))print(s.rindex('one',0,6))  
#指定起始和结束位置#输出:8#     0
Copy after login

rjust(Specify the length and padding character, the content will be right-aligned, and the padding character will be left blank if it is a space)

s = 'Hello'print(s.rjust(10,'-'))
#输出:-----Hello
Copy after login

rpartition(Specify the separator, start the characters from the right String is divided)

s = 'IamTom_IamTom'print(s.rpartition('am'))
#输出:('IamTom_I', 'am', 'Tom')
Copy after login

rsplit(指定分隔符对字符串进行切片,如果指定第二个参数num,则只分隔num次,最后返回一个列表)

s = 'a b c d'print(s.rsplit())print(s.rsplit(' ',2))  
#从右边开始,按空格分隔两次#输出:['a', 'b', 'c', 'd']
#     ['a b', 'c', 'd']
Copy after login

rstrip(删除字符串末尾的指定字符,默认为空格)

s = '!!! I am Tom !!!'print(s.rstrip('!'))
#输出:!!! I am Tom
Copy after login

split(指定分隔符对字符串进行切片,如果指定第二个参数num,则只分隔num次,最后返回一个列表)

s = 'a b c d'print(s.split())print(s.split(' ',2)) 
#从左边开始,按空格分隔两次#输出:['a', 'b', 'c', 'd']#    ['a', 'b', 'c d']
Copy after login

splitlines(按换行符来分隔字符串,返回一个列表)

s = 'a\nb\nc'print(s.splitlines())    
#默认参数为Falseprint(s.splitlines(True)) 
#指定Ture参数,则保留换行符#输出:['a', 'b', 'c']
#     ['a\n', 'b\n', 'c']
Copy after login

startswith(判断字符串是否以某个字符或字符串开头的,第二个参数:起始位置,第三个参数:结束位置)

s = 'Projects'print(s.startswith('Pr'))print(s.startswith('e',4,8))
#输出:True#     True
Copy after login

strip(删除字符串前后的指定字符,默认为空格)

s = '!!! I am Tom !!!'print(s.strip('!'))
#输出: I am Tom
Copy after login

swapcase(大小写互换)

s = 'I am Tom'print(s.swapcase())
#输出:i AM tOM
Copy after login

title(转换成标题,就是每个单词首字母大写)

s = 'i am tom'print(s.title())
#输出:I Am Tom
Copy after login

translate(根据maketrans方法创建的表,进行字符替换)

intab = "abcde"outtab = "12345"trantab = str.maketrans(intab, outtab)
str = "Hello abc"print (str.translate(trantab))
#输出:H5llo 123
Copy after login
Copy after login

upper(小写转换成大写)

s = 'Hello'print(s.upper())
#输出:HELLO
Copy after login

zfill(指定字符串的长度。原字符串右对齐,前面填充0)

s = 'Hello'print(s.zfill(10))
# 输出:00000Hello
Copy after login

The above is the detailed content of Detailed explanation of the functions of python3 strings with examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template