编程语言
首页 > 编程语言> > python -- 字符串的各种用法

python -- 字符串的各种用法

作者:互联网

输入:

print("1:",'3'.isdigit())    #判断是否整数,整数位True,其他False(包括小数)
print("2:",'China'.islower(),'china'.islower(),'2'.islower()) #判断是否全为小写
print("3:",''.isspace(),' '.isspace(),' '.isspace(),' 1 '.isspace()) #判断是否为空格
print("4:",'CHINA'.isupper(),'China'.isupper(),'3'.isupper()) #判断是否全为小写
n = [1,2,3,4,5]
m = ['1','2','3','4','5']
print("5:",''.join(m),'-'.join(m),','.join(m)) #拼接列表元素(元素类型只能是字符串,不可以为数字)
a = 'Happy New Year!!!'
print("6:",a.ljust(50,'*')) #向左对齐,不足宽度的,用指定符号向右补充
print("7:",a.rjust(50,'+')) #向右对齐,不足宽度的,用指定符号向左补充
print("8:",'China'.lower(),'CHINA'.lower(),'2'.lower()) #将大写转为小写
print("9:",'China'.upper(),'china'.upper(),'5'.upper()) #将小写转为大写
b = '\tHappy New Year \n'
print("10.1:",b)
print("10.2:",b.strip()) #去掉左右的转义字符及空格,留下字符串(中间的转义字符及空格无法去除)
print("11:",b.lstrip()) #去掉左的转义字符及空格,留下字符串
print("12:",b.rstrip()) #去掉右的转义字符及空格,留下字符串
print("13:",a.replace("!",".",2)) #替换前字符,替换后字符,替换次数
c = 'Tom-Mary-Sam-Peter-Jason-Mike'
print("14.1:",c.split('-')) #以指定字符为拆分符号,将字符串拆分为列表
print("14.2:",c.split('-',2)) #指定字符为拆分符号,并指定从左起拆分次数
d = 'Tom|Mary|Sam|Peter|Jason|Mike'
print("15:",d.rsplit('|',3)) #指定字符为拆分符号,并指定从右起拆分次数
print("16:",d.startswith('To'),d.startswith('t')) #判断字符串是否以某字符或某部分字符串开头(区分大小写)
print("17:",d.endswith('e'),d.endswith('ikE')) #判断字符串是否以某字符或某部分字符串结尾(区分大小写)
print("18:",a.swapcase()) #大小写互换
print("19:",a.zfill(60)) #指定字符串长度,不足的用0补充
print("20:",a[0]) #字符串类似列表,有下标数,可通过下标数取对应位置的值
print("21:",a[::2]) #字符串步长切片
print("22:",a[0:5:3]) #字符串指定范围步长切片
print("23:",a[::-1],a) #字符串反转(原字符串不变,只是输出反转后的值,并非给原字符串重新赋值)
e = '\tTony' + '\n'
print("24:",e) #字符串可以相加
e = r'\tTony\n'
print("25:",e) #字符串前加r,转义字符视为普通字符
f = 'bruce leE!!!4'
print("26:",f.capitalize()) #首字母大小
print("27:",f.casefold()) #
print("28:",f.center(50,'=')) #用指定字符在左右补充到指定宽度
print("29:",f.find('!'),f.find('k')) #找出字符串中第一个匹配指定值对应的下标数,不存在则返回-1
g = "How %s are %s?" %('old','you') #占位符,需要按顺序填写
print("30:",g)
h = 'How {word01} {word02} {word01}?'
print("30:",h.format(word02='you',word01='do')) #format使用方法,指定了变量名称,赋值时不需要按顺序

======================================================================================================================
输出:

1: True
2: False True False
3: False True True False
4: True False False
5: 12345 1-2-3-4-5 1,2,3,4,5
6: Happy New Year!!!*********************************
7: +++++++++++++++++++++++++++++++++Happy New Year!!!
8: china china 2
9: CHINA CHINA 5
10.1: Happy New Year

10.2: Happy New Year
11: Happy New Year

12: Happy New Year
13: Happy New Year..!
14.1: ['Tom', 'Mary', 'Sam', 'Peter', 'Jason', 'Mike']
14.2: ['Tom', 'Mary', 'Sam-Peter-Jason-Mike']
15: ['Tom|Mary|Sam', 'Peter', 'Jason', 'Mike']
16: True False
17: True False
18: hAPPY nEW yEAR!!!
19: 0000000000000000000000000000000000000000000Happy New Year!!!
20: H
21: HpyNwYa!!
22: Hp
23: !!!raeY weN yppaH Happy New Year!!!
24: Tony

25: \tTony\n
26: Bruce lee!!!4
27: bruce lee!!!4
28: ==================bruce leE!!!4===================
29: 9 -1
30: How old are you?
30: How do you do?

标签:python,指定,--,字符串,Year,print,New,Happy
来源: https://www.cnblogs.com/csy113/p/15617102.html