标签:cas 索引 ber 语言 换行符 定时 asc str 结束
字符串相关的功能详解
t = (‘heLLo‘)
v = t.capitalize()print(v)
输出:
Hello
其效果和 lower() 方法非常相似,都可以转换字符串中所有大写字符为小写。
两者的区别是:lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法。
t = (‘HELLo‘)
v = t.casefold()
print(v)
输出:
hello
t = (‘hello‘)
v = t.center(20)
print(v)
#输出:
hellot = (‘hello‘)
v = t.center(20, ‘*‘)
print(v)
#输出:
*******hello********
t = (‘hello‘)
v = t.count(‘l‘)
print(v)
#输出:
2
#!/usr/bin/python3
str = "菜鸟教程";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
print(str)
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
print("UTF-8 解码:", str_utf8.decode(‘UTF-8‘,‘strict‘))
print("GBK 解码:", str_gbk.decode(‘GBK‘,‘strict‘))输出:菜鸟教程
UTF-8 编码: b‘\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b‘
GBK 编码: b‘\xb2\xcb\xc4\xf1\xbd\xcc\xb3\xcc‘
UTF-8 解码: 菜鸟教程
GBK 解码: 菜鸟教程
t = ‘Hello‘
v1 = t.endswith(‘o‘)
v2 = t.endswith(‘l‘)
print(v1,v2)输出:True False
t = ‘username\tadress\tage\nBob\tNewYork\t12\nLily\tShanghai\t22‘
v = t.expandtabs(20)
print(v)输出:username adress age
Bob NewYork 12
Lily Shanghai 22
t = ‘Hello‘
v1 = t.find(‘o‘)
v2 = t.find(‘t‘)
print(v1, v2)输出:
4 -1
v1 = ‘my name is {} ,age {}‘.format(‘hoho‘,18)
v2 = (‘my name is %s ,age %d ‘ % (‘hoho‘ ,18))
print(v1)
print(v2)输出:my name is hoho ,age 18
my name is hoho ,age 18
t = ‘Hello‘
v1 = t.index(‘o‘)
print(v1)输出:
4t = ‘Hello‘
v1 = t.index(‘a‘)
print(v1)输出:Traceback (most recent call last):
File "C:/Users/ehaier/PycharmProjects/old_boy/基础/t1.py", line 4, in <module>
v1 = t.index(‘a‘)
ValueError: substring not found
t1 = ‘Hello234_‘
t2 = ‘Hello123‘
v1 = t1.isalnum()
print(v1)
v1 = t2.isalnum()
print(v1)输出:
False
True
t1 = ‘Hello1234‘
t2 = ‘Hello‘
v1 = t1.isalpha()
print(v1)
v1 = t2.isalpha()
print(v1)输出:False
True
t1 = ‘12.34‘
t2 = ‘1234‘
v1 = t1.isdecimal()
print(v1)
v1 = t2.isdecimal()
print(v1)输出:False
True
t1 = ‘123345‘
t2 = ‘hello123‘
v1 = t1.isdigit()
print(v1)
v1 = t2.isdigit()
print(v1)
输出:True
False
t1 = ‘def‘
t2 = ‘1def‘
v1 = t1.isidentifier()
v2 = t2.isidentifier()
print(v1)
print(v2)
输出;True
False
t1 = ‘hello‘
t2 = ‘Hello‘
v1 = t1.islower()
v2 = t2.islower()
print(v1)
print(v2)
输出:True
False
t1 = ‘二‘
t2 = ‘123‘
v1 = t1.isnumeric()
v2 = t2.isnumeric()
print(v1)
print(v2)
输出:True
True
t1 = ‘Hello\t‘
t2 = ‘Hello‘
v1 = t1.isprintable()
v2 = t2.isprintable()
print(v1)
print(v2)
输出:False
True
t1 = ‘Hello ‘
t2 = ‘ ‘
v1 = t1.isspace()
v2 = t2.isspace()
print(v1)
print(v2)
输出:False
True
t1 = ‘Hello ,everyone!‘
t2 = ‘Hello, Everyone!‘
v1 = t1.istitle()
v2 = t2.istitle()
print(v1)
print(v2)
输出;False
True
t1 = ‘Hello‘
t2 = ‘HELLO‘
v1 = t1.isupper()
v2 = t2.isupper()
print(v1)
print(v2)
输出:False
True
t1 = ‘hello‘
t2 = ‘_‘
v1 = t2.join(t1)
print(v1)
输出:
h_e_l_l_o
t1 = ‘hello‘
t2 = ‘‘
v1 = t1.ljust(20,‘*‘)
print(v1)
输出:hello***************
t1 = ‘HEllo‘
t2 = ‘‘
v1 = t1.lower()
print(v1)
输出:
hello
t1 = ‘HEllo‘
v1 = t1.lstrip(‘H‘)
print(v1)
输出:
Ellot1 = ‘ HEllo ‘
v1 = t1.lstrip()
print(v1)
输出:
HEllo
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
str = "this is string example....wow!!!"
print (str.translate(trantab))
输出;
th3s 3s str3ng 2x1mpl2....w4w!!!
t1 = ‘HEllo‘
v1 = t1.partition(‘l‘)
print(v1)
输出;
(‘HE‘, ‘l‘, ‘lo‘)
t1 = ‘HEllo‘
v1 = t1.replace(‘l‘, ‘a‘)
print(v1)
输出:
HEaao
t1 = ‘HEllo‘
v1 = t1.rfind(‘l‘)
print(v1)
输出:
3
t1 = ‘HEllo‘
v1 = t1.rindex(‘l‘)
print(v1)
输出:
3
t1 = ‘hello‘
t2 = ‘‘
v1 = t1.rjust(20,‘*‘)
print(v1)
输出:
***************hello
t1 = ‘HEllo‘
v1 = t1.rpartition(‘l‘)
print(v1)
输出:
(‘HEl‘, ‘l‘, ‘o‘)
S
=
"this is string example....wow!!!"
(S.rsplit( ))
(S.rsplit(
‘i‘
,
1
))
(S.rsplit(
‘w‘
))
输出:[‘this‘, ‘is‘, ‘string‘, ‘example....wow!!!‘]
[‘this is str‘, ‘ng example....wow!!!‘]
[‘this is string example....‘, ‘o‘, ‘!!!‘]
t1 = ‘ HEllo ‘
v1 = t1.rstrip()
print(v1)
输出:
HEllot1 = ‘HEllo‘
v1 = t1.rstrip(‘o‘)
print(v1)
输出:
HEll
S = "this is string example....wow!!!"
print (S.split( ))
print (S.split(‘i‘,1))
print (S.split(‘w‘))
输出:[‘this‘, ‘is‘, ‘string‘, ‘example....wow!!!‘]
[‘th‘, ‘s is string example....wow!!!‘]
[‘this is string example....‘, ‘o‘, ‘!!!‘]
t = ‘ab c\n\nde fg\rkl\r\n‘
v1 = t.splitlines()
v2 = t.splitlines(True)
print(v1)
print(v2)
输出:[‘ab c‘, ‘‘, ‘de fg‘, ‘kl‘]
[‘ab c\n‘, ‘\n‘, ‘de fg\r‘, ‘kl\r\n‘]
t1 = ‘HEllo‘
v1 = t1.startswith(‘h‘)
v2 = t1.startswith(‘H‘)
print(v1)
print(v2)
输出;False
True
t = ‘ hello ‘
v = t.strip()
print(v)
输出:
hello
t = ‘hEllO‘
v = t.swapcase()
print(v)
输出:
HeLLo
t = ‘hello, every one‘
v = t.title()
print(v)
输出:
Hello, Every One
t = ‘hello‘
v = t.upper()
print(v)
输出:
HELLO
str = "this is string example from runoob....wow!!!"
print ("str.zfill : ",str.zfill(40))
print ("str.zfill : ",str.zfill(50))
输出:str.zfill : this is string example from runoob....wow!!!
str.zfill : 000000this is string example from runoob....wow!!!
标签:cas 索引 ber 语言 换行符 定时 asc str 结束
原文地址:https://www.cnblogs.com/yujiaershao/p/9984456.html