一、字符串的内置方法
1. 字符串-大小写转换,方法为:lower(), upper()
old_code = 'KeViN'
print ( '这是返回给用户的验证码: %s' % old_code)
new_code = input ( '请输入你的验证码:' ) . strip( )
print ( new_code)
if old_code. lower( ) == new_code. lower( ) :
print ( '验证码输入正确' )
else :
print ( '验证码输入错误' )
2. 字符串-判断字符串是否以括号内指定的字符开头或结尾,结果为布尔值True或False,方法为:startswith(), endswith()
res = 'hello oldBoy'
print ( res. startswith( 'h' ) )
print ( res. startswith( 'hel' ) )
print ( res. startswith( 'hello1' ) )
print ( res. endswith( 'y' ) )
print ( res. endswith( 'Boy' ) )
print ( res. endswith( 'boy' ) )
3.字符串- 格式化输出之format
第一种方式:类似于%s的用法,传入的值会按照位置与{}一一对应
res = 'my name is {}, my age is {},{},{}'
print ( res. format ( 'tank' , 18 ) )
第二种方式:可以写索引,并且索引也可以反复使用
res = 'my name is {0}, my age is {1},{0},{1}, {2}'
print ( res. format ( 'kevin' , 18 , 'aa' ) )
第三种方式:format括号内在传参数时完全可以打乱顺序,但仍然能指名道姓地为指定的参数传值
res = 'my name is {name1}, my age is {age1}, {name1}, {name1}, {age1}'
print ( res. format ( name1= 'kevin' , age1= 18 ) )
4. 字符串-join():从可迭代对象中取出多个字符串,然后按照指定的分隔符进行拼接,拼接的结果为字符串
l = [ 'kevin' , 'jack' , 'tank' , 'tony' , 'kevin' , 'jack' , 'tank' , 'tony' , 'kevin' , 'jack' , 'tank' , 'tony' , 'kevin' , 'jack' , 'tank' , 'tony' ]
print ( '|' . join( l) )
5. 字符串-replace():替换,指定修改的个数或用新的字符替换字符串中旧的字符
res = 'my name is jack jack jack jack'
print ( res. replace( 'jack' , 'kevin' ) )
print ( res. replace( 'jack' , 'kevin' , 1 ) )
print ( res. replace( 'jack' , 'kevin' , 2 ) )
m = '10'
n = '20'
res = m + n
m = res. replace( '10' , '' )
n = res. replace( '20' , '' )
print ( m, n)
6. 字符串-isdigit():判断字符串是否是纯数字组成,返回结果为True或False
guess = input ( '请输入你的年龄:' ) . strip( )
if guess. isdigit( ) :
guess = int ( guess)
else :
print ( '你什么玩意,能不能好好输入' )
二、字符串了解的方法
1.字符串-find,rfind,index,rindex,count
msg = 'tony say hello'
print ( msg. find( 'o' ) )
print ( msg. find( 's' ) )
print ( msg. find( 'hello' ) )
print ( msg. find( 'helloworld' , 1 , 5 ) )
print ( msg. rfind( 'o' ) )
print ( msg. index( 'e' ) )
print ( msg. rindex( 'n' ) )
print ( msg. index( 'ee' ) )
print ( msg. count( 'say' ) )
2. 字符串-center,ljust,rjust,zfill
name = 'kevin'
print ( name. center( 30 , '*' ) )
print ( name. ljust( 30 , '*' ) )
print ( name. rjust( 30 , '*' ) )
print ( name. zfill( 30 ) )
3. 字符串-capitalize(首字母大写),swapcase(大小写翻转),title(每个单词的首字母大写)
message = 'hello everyone, Nice to meet you!'
print ( message. capitalize( ) )
print ( message. swapcase( ) )
print ( message. title( ) )
print ( message. islower( ) )
print ( message. isupper( ) )
三、列表的内置方法
1.列表-类型转换
print ( list ( 'hello' ) )
print ( list ( [ 1 , 2 , 3 , 4 ] ) )
print ( list ( { 'username' : 'kevin' , 'age' : 18 , 'sex' : '女' } ) )
print ( list ( ( 1 , 2 , 3 , 4 ) ) )
print ( list ( { 1 , 2 , 3 , 4 } ) )
2. 列表-添加元素
方式1:append()在末尾追加元素
name_list = [ 'kevin' , 'tony' , 'tank' , 'jack' ]
res = name_list. append( 'you' )
print ( res)
print ( name_list)
name_list. append( [ 11 , 22 , 33 ] )
print ( name_list)
方式2:insert()在对应的索引位置添加元素
name_list = [ 'kevin' , 'tony' , 'tank' , 'jack' ]
name_list. insert( 0 , 666 )
print ( name_list)
name_list. insert( 2 , '999感冒灵' )
print ( name_list)
name_list. insert( 3 , [ 1 , 2 , 3 ] )
print ( name_list)
方式3:extend扩展元素,是用来做列表的合并的
name_list = [ 'kevin' , 'tony' , 'tank' , 'jack' ]
lst = [ '春' , '夏' , '秋' , '冬' ]
for i in lst:
name_list. append( i)
print ( name_list)
3.列表- 删除元素
方式1:del: 删除索引对应的元素
name_list = [ 'kevin' , 'tony' , 11 , 22 , 33 , 'tank' , 'jack' ]
del name_list[ 3 ]
print ( name_list)
方式2:remove() 括号内指名道姓表示要删除哪个元素,没有返回值
name_list = [ 'kevin' , 'tony' , 11 , 22 , 33 , 'tank' , 'jack' ]
name_list. remove( 'tank' )
print ( name_list)
方式3:pop()弹出元素
name_list = [ 'kevin' , 'tony' , 11 , 22 , 33 , 'tank' , 'jack' ]
res = name_list. pop( )
print ( res)
res = name_list. pop( 3 )
print ( res)
print ( name_list)
4.列表- 需要了解的方法 :sort()方法,排序时列表元素之间必须是相同数据类型,不可混搭,否则报错
lst = [ 4 , 2 , 6 , 8 , 54 , 37 , 45 ]
lst. sort( )
print ( lst)
lst. sort( reverse= True )
print ( lst)
s = [ 'kevin' , 'jack' , 'xu' , 'tank' ]
s. sort( )
print ( s)
四、可变类型与不可变类型
res = 'helloworld'
print ( id ( res) )
res1 = res. title( )
print ( id ( res1) )
print ( res. title( ) )
print ( res)
name_list = [ 'kevin' , 'tank' , 'jack' ]
print ( name_list)
name_list. append( 'tony' )
print ( name_list)
print ( id ( name_list) )
五、队列和栈的概念
链表、单链表、双链表、循环链表、栈、队列、树、二叉树、平衡二叉树、红黑树、b树、b+ 树、b- 树、图