标签:连接符 bst value order spring spl string 实现 --
int转str:str(digit)
str转int:int(string) 注意此处的string只能是包含数字的字符串或开头有负号
bool(非零)-->True
bool(零) -->False
int(True) -->1
int(False)-->0
bool -->str没有实际意义
str -->bool:
bool(非空字符串) --> True
bool(空字符串) --> False
注意:空字符串为'',而不是' '。
>>> str1 = 'python'
>>> str1[2]
't'
>>> str1[-1]
'n'
>>> str2 = 'python'
>>> str2[0 : -1]
'pytho'
>>> str2[0:]
'python'
>>> str3 = 'python is a language'
>>> str3[::2]
'pto salnug'
>>> str3[::-1]
'egaugnal a si nohtyp'
当步长为负时,是倒着取值。
另外可以借助数组来实现把字符串倒序
strA = input("请输入需要翻转的字符串:")
order = []
for i in strA:
order.append(i)
order.reverse() #将列表反转
print ''.join(order) #将list转换成字符串
>>> str4 = 'hellokitty'
>>> str4.capitalize()
'Hellokitty'
>>> str4 = 'hellokitty'
>>> str4.center(20, '*')
'*****hellokitty*****'
>>> str4 = 'hello kitty'
>>> str4.upper()
'HELLO KITTY'
>>> str5 = 'Hello Kitty'
>>> str5.lower()
'hello kitty'
>>> str5 = 'Hello Kitty'
>>> str5.swapcase()
'hELLO kITTY'
>>> str6 = 'hello kitty,what can I do4you?'
>>> str6.title()
'Hello Kitty,What Can I Do4You?'
>>> str6 = 'hello kitty,what can I do4you?'
>>> str6.startswith('h')
True
>>> str6.startswith('hello')
True
>>> str6 = 'hello kitty,what can I do4you?'
>>> str6.find('h')
0
>>> str6.find('what')
12
>>> str6.find('.')
-1
>>> str6 = 'hello kitty,what can I do4you?'
>>> str6.index('h')
0
>>> str6.index('what')
12
>>> str6.index('.')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> str7 = "\tI'm a superman. "
>>> str7.strip()
"I'm a superman."
>>> str7 = '==hey guys, you shutup!=='
>>> str7.strip('==')
'hey guys, you shutup!'
lstrip()是去除左边的。
rstrip()是去除右边的。
>>> str8 = 'spring summer autumn winter'
>>> str8.split()
['spring', 'summer', 'autumn', 'winter']
>>> str8 = 'spring,summer,autumn,winter'
>>> str8.split(',')
['spring', 'summer', 'autumn', 'winter']
一般有n个分割符,就有n+1个列表元素。
但有个特例:当字符串开头有一个空格时并不指定分割符,就只会有n个列表元素。
>>> str8 = ' spring summer autumn winter'
>>> str8.split()
['spring', 'summer', 'autumn', 'winter']
>>> str8.split(' ')
['', 'spring', 'summer', 'autumn', 'winter']
>>> str8.split(',', 1)
['spring', 'summer,autumn,winter']
可以用在分割路径与文件名: ★★★★★
>>> file_path = r'E:\aaa\bbb\ccc.ddd'
>>> path, file = file_path.rsplit('\\', 1)
>>> path
'E:\\aaa\\bbb'
>>> file
'ccc.ddd'
>>> s1 = 'abc'
>>> '_'.join(s1)
'a_b_c'
>>> s2 = "Jack is Mary's boy friend."
>>> s2.replace('Jack', 'Mark')
"Mark is Mary's boy friend."
>>> s2.replace('a', 'o', 1)
"Jock is Mary's boy friend."
s1 = '我叫{},今年{},性别{}'
三种方式
第一种
s2 = '我叫{},今年{},性别{}'.format('太白','28','男')
print(s2)
第二种
s3 = '我叫{0},今年{1},性别{2},我依然叫{0}'.format('太白', '28', '男')
print(s3)
第三种
s4 = '我叫{name},今年{age},性别{sex}'.format(age='28', name='太白', sex='男')
print(s4)
name = 'taibai'
name1 = 'a123'
print(name.isalnum()) # 数字或字母组成
print(name1.isdigit()) # 判断全部是由整数组成
print(name.isalpha()) # 全部由字母组成
>>> name='alexaaaa'
>>> name.count('a') #可以设置起始位置,结束位置。
5
>>> print(len(name))
8
标签:连接符 bst value order spring spl string 实现 --
原文地址:https://www.cnblogs.com/lanhoo/p/9432328.html