name='xiao ming'
names=name.capitalize()
print(names)
>>>
Xiao ming
name='xiao ming'
names=name.title()
print(names)
>>>
Xiao Ming
name='xiao ming'
names=name.istitle()
print(names)
>>>
False
name='Xiao Ming'
names=name.istitle()
print(names)
>>>
True
name='xiao ming'
names=name.upper()
print(names)
>>>
XIAO MING
name='XIAO MING'
names=name.lower()
print(names)
>>>
xiao ming
name='xiao ming'
print(len(name))
name='xiaoming'
print(len(name))
>>>9
>>>8
例:找到要找的字符报出下标(所谓下标是左边下标开始数起到目的下标)没找到要找的字符,报异常!报结果为-1
-----找到字符串“n”
name='https://w.baidu.com/img/bd_logo|.png'
names=name.rfind('n')
print(names)
>>> 34 #指的是要找的"n"的下标
-----没找到字符串“e”
name='https://w.baidu.com/img/bd_logo|.png'
names=name.rfind('e')
print(names)
>>> -1 #没找到 “e”,报错-1
又例:要截取bd_logo|.png
name='https://w.baidu.com/img/bd_logo|.png'
names=name.rfind('/')
print(names)
M1=name[names+1:]
print(M1)
>>> bd_logo|.png
例:要截取/w.baidu.com/img/bd_logo|.png
name='https://w.baidu.com/img/bd_logo|.png'
names=name.find('/')
print(names)
M1=name[names+1:]
print(M1)
>>> /w.baidu.com/img/bd_logo|.png
例:要截取png
name='https://w.baidu.com/img/bd_logo|.png'
names=name.rfind('.')
print(names)
M1=name[names+1:]
print(M1)
>>> png
例:找到字符串“n”
name='https://w.baidu.com/img/bd_logo|.png'
names=name.index('n')
print(names)
>>>34
-----没找到字符串“e”
name='https://w.baidu.com/img/bd_logo|.png'
names=name.index('e')
print(names)
>>>ValueError: substring not found #报错
例:要截取baidu.com/img/bd_logo|.png
name='https://w.baidu.com/img/bd_logo|.png'
names=name.index('.')
print(names)
M1=name[names+1:]
print(M1)
>>>baidu.com/img/bd_logo|.png
name='张三,李四,王五,张三,张三,holle world'
names=name.replace(' ','$')
print(names)
>>> 张三,李四,王五,张三,张三,holle$world
例:从左到右,将第一个张三换小吴
name='张三,李四,王五,张三,张三,holle world'
names=name.replace('张三','小吴',1)
print(names)
>>>小吴,李四,王五,张三,张三,holle world
例:从左到右,将第一个和第二个张三换小吴
name='张三,李四,王五,张三,张三,holle world'
names=name.replace('张三','小吴',2)
print(names)
>>>
小吴,李四,王五,小吴,张三,holle world
例:将所有的张三换成小吴
name='张三,李四,王五,张三,张三,holle world'
names=name.replace('张三','小吴')
print(names)
>>>
小吴,李四,王五,小吴,小吴,holle world
例如:encode编码: ‘上课啦!认真听课!’
name='上课啦!认真听课!'
names=name.encode('utf-8')
print(names)
>>>b'\xe4\xb8\x8a\xe8\xaf\xbe\xe5\x95\xa6\xef
\xbc\x81\xe8\xae\xa4\xe7\x9c\x9f\xe5\x90\xac\xe8\xaf\xbe\xef\xbc\x81'
decode解码:b’\xe4\xb8\x8a\xe8\xaf…
L1=names.decode('utf-8')
print(L1)
>>>上课啦!认真听课!
获取字符的编码值
print(ord("A"))
print(ord("真"))
>>>65
>>>30495
根据码值获取指定字符
print(chr(65))
print(chr(30495))
>>>A
>>>真
例:判断是否以1开头
name='1.doc'
result=name.startswith('1')
print(result)
>>>True
例:判断是否以2开头
name='1.doc'
result=name.startswith('2')
print(result)
>>>False
例:判断是否以’doc’结尾
name='1.doc'
result=name.endswith('doc')
print(result)
>>>True
例:判断是否以’txt’结尾
name='1.doc'
result=name.endswith('txt')
print(result)
>>>False
例:判断所有的字符是否都是字母
name='dsdoc'
result=name.isalpha()
print(result)
>>>True
name='dsdoc1'
result=name.isalpha()
print(result)
>>>False
例:判断所有的字符是否都是数字
name='12555'
result=name.isdigit()
print(result)
>>>True
name='12cdf'
result=name.isdigit()
print(result)
>>>False
例:判断所有的字符是否由字母或数字组成
name='12cdf'
result=name.isalnum()
print(result)
>>>True
name=['a','b','c']
result='-'.join(name)
print(result)
>>>a-b-c
result='-'.join('abc')
print(result)
>>>a-b-c
name=['a','b','c']
result=''.join(name)
print(result)
>>>abc
name='hcdf'
result=max(name)
print(result)
>>>h
name='hcdf'
result=min(name)
print(result)
>>>c
例:去除字符左侧空格
L1=' 你好'
L2=L1.lstrip()
print(L2)
>>>你好
例:去除右侧空格
L1='你好 '
L2=L1.rstrip()
print(L2)
>>>你好
例:去除字符两侧侧空格
L1=' 你好 '
L2=L1.strip()
print(L2)
>>>你好
还有另一种用法:去除字符最左侧字符
L1='他你好啊'
L2=L1.lstrip('他')
print(L2)
>>>你好啊
同理:去除字符最右侧字符
L1='他你好啊'
L2=L1.rstrip('啊')
print(L2)
>>>他你好
L1='ddkskm\ndds cedsss\nddw\nd'
L2=L1.split()
print(L2)
>>>['ddkskm', 'dds', 'cedsss', 'ddw', 'd']
例:当有各种分割符时,想截取需要的部分,如需截取cedsss
L1='ddkskm\ndds cedsss\nddw\nd'
L2=L1.split()[2]
print(L2)
>>>cedsss
split(选中\n,分割个数为2)
L1='ddkskm\ndds cedsss\nddw\nd'
L2=L1.split('\n',2)
print(L2)
>>>['ddkskm', 'dds cedsss', 'ddw\nd']
split(选中空格,分割个数为2)
name='a b c d e'
names=name.split(' ',2)
print(names)
>>>['a', 'b', 'c d e']
name='张三,李四,王五,张三,张三,holle world'
result=name.count('张三')
print(result)
>>>3
L1='11111111111'
L2=L1.ljust(20)
print(L2)
>>>11111111111
例:右对齐
L1='11111111111'
L2=L1.rjust(20)
print(L2)
>>> 11111111111