菜鸟教程Python3实例刷题 51-64
51
str = "Runoob"
place = int ( input ( "输入位置: " ) )
new_str = str . replace( str [ place] , "" , 1 )
print ( new_str)
52
string = "www.runoob.com"
sub_str = "runoob"
if sub_str in string:
print ( '存在' )
else :
print ( '不存在' )
53
str = "runoob"
print ( len ( str ) )
54
import re
def Find ( string) :
url = re. findall( 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+' , string)
return url
string = 'Runoob 的网页地址为:https://www.runoob.com,Google 的网页地址为:https://www.google.com'
print ( "Urls: " , Find( string) )
55
str = "Runoob"
str1 = '' . join( reversed ( str ) )
print ( str1)
str2 = str [ : : - 1 ]
print ( str2)
56
def rotate ( input , d) :
Lfirst = input [ 0 : d]
Lsecond = input [ d: ]
Rfirst = input [ 0 : len ( input ) - d]
Rsecond = input [ len ( input ) - d: ]
print ( "头部切片翻转 : " , ( Lsecond + Lfirst) )
print ( "尾部切片翻转 : " , ( Rsecond + Rfirst) )
input = 'Runoob'
d = 2
rotate( input , d)
57
def dictionary ( ) :
key_value= { }
key_value[ 2 ] = 15
key_value[ 1 ] = 2
key_value[ 5 ] = 12
key_value[ 4 ] = 24
key_value[ 6 ] = 18
key_value[ 3 ] = 323
print ( "按key排序: " )
for i in sorted ( key_value) :
print ( i, key_value[ i] )
def main ( ) :
dictionary( )
if __name__ == '__main__' :
main( )
58
def returnSum ( dic) :
sum = 0
for i in dic:
sum = sum + dic[ i]
return sum
dic = { 'a' : 10 , 'b' : 20 , 'c' : 30 }
print ( returnSum( dic) )
59
test_dict = { "Runoob" : 1 , "Google" : 2 , "Taobao" : 3 , "Zhihu" : 4 }
print ( "字典移除前 : " + str ( test_dict) )
del test_dict[ 'Zhihu' ]
print ( "字典移除后 : " + str ( test_dict) )
60
def Merge ( dict1, dict2) :
dict2. update( dict1)
dict1 = { 'a' : 10 , 'b' : 8 }
dict2 = { 'd' : 6 , 'c' : 4 }
print ( Merge( dict1, dict2) )
print ( dict2)
61
import time
a1 = "2020-5-10 23:40:00"
timeArray = time. strptime( a1, "%Y-%m-%d %H:%M:%S" )
print ( type ( timeArray) )
timeStamp = int ( time. mktime( timeArray) )
print ( type ( timeStamp) )
print ( timeStamp)
a2 = "2020/5/10 23:40:00"
timeArray = time. strptime( a2, "%Y/%m/%d %H:%M:%S" )
print ( type ( timeArray) )
otherStyleTime = time. strftime( "%Y/%m/%d %H:%M:%S" , timeArray)
print ( type ( otherStyleTime) )
print ( otherStyleTime)
62
import datetime
threeDayAgo = datetime. datetime. today( ) - datetime. timedelta( 3 )
print ( threeDayAgo, type ( threeDayAgo) )
otherStyleTime = threeDayAgo. strftime( "%Y-%m-%d %H:%M:%S" )
print ( otherStyleTime, type ( otherStyleTime) )
63
import datetime
now = datetime. datetime. now( )
print ( now, type ( now) )
otherStyleTime = now. strftime( "%Y-%m-%d %H:%M:%S" )
print ( otherStyleTime, type ( otherStyleTime) )
print ( '\n' )
import time
now = int ( time. time( ) )
print ( now, type ( now) )
timeArray = time. localtime( now)
print ( timeArray, type ( timeArray) )
otherStyleTime = time. strftime( "%Y-%m-%d %H:%M:%S" , timeArray)
print ( otherStyleTime, type ( otherStyleTime) )
64
name = input ( "输入你的名字[仅限英文]: \n\n" )
lngth = len ( name)
l = ""
for x in range ( 0 , lngth) :
c = name[ x]
c = c. upper( )
if ( c == "A" ) :
print ( "..######..\n..#....#..\n..######.." , end= " " )
print ( "\n..#....#..\n..#....#..\n\n" )
elif ( c == "B" ) :
print ( "..######..\n..#....#..\n..#####..." , end= " " )
print ( "\n..#....#..\n..######..\n\n" )
elif ( c == "C" ) :
print ( "..######..\n..#.......\n..#......." , end= " " )
print ( "\n..#.......\n..######..\n\n" )
elif ( c == "D" ) :
print ( "..#####...\n..#....#..\n..#....#.." , end= " " )
print ( "\n..#....#..\n..#####...\n\n" )
elif ( c == "E" ) :
print ( "..######..\n..#.......\n..#####..." , end= " " )
print ( "\n..#.......\n..######..\n\n" )
elif ( c == "F" ) :
print ( "..######..\n..#.......\n..#####..." , end= " " )
print ( "\n..#.......\n..#.......\n\n" )
elif ( c == "G" ) :
print ( "..######..\n..#.......\n..#.####.." , end= " " )
print ( "\n..#....#..\n..#####...\n\n" )
elif ( c == "H" ) :
print ( "..#....#..\n..#....#..\n..######.." , end= " " )
print ( "\n..#....#..\n..#....#..\n\n" )
elif ( c == "I" ) :
print ( "..######..\n....##....\n....##...." , end= " " )
print ( "\n....##....\n..######..\n\n" )
elif ( c == "J" ) :
print ( "..######..\n....##....\n....##...." , end= " " )
print ( "\n..#.##....\n..####....\n\n" )
elif ( c == "K" ) :
print ( "..#...#...\n..#..#....\n..##......" , end= " " )
print ( "\n..#..#....\n..#...#...\n\n" )
elif ( c == "L" ) :
print ( "..#.......\n..#.......\n..#......." , end= " " )
print ( "\n..#.......\n..######..\n\n" )
elif ( c == "M" ) :
print ( "..#....#..\n..##..##..\n..#.##.#.." , end= " " )
print ( "\n..#....#..\n..#....#..\n\n" )
elif ( c == "N" ) :
print ( "..#....#..\n..##...#..\n..#.#..#.." , end= " " )
print ( "\n..#..#.#..\n..#...##..\n\n" )
elif ( c == "O" ) :
print ( "..######..\n..#....#..\n..#....#.." , end= " " )
print ( "\n..#....#..\n..######..\n\n" )
elif ( c == "P" ) :
print ( "..######..\n..#....#..\n..######.." , end= " " )
print ( "\n..#.......\n..#.......\n\n" )
elif ( c == "Q" ) :
print ( "..######..\n..#....#..\n..#.#..#.." , end= " " )
print ( "\n..#..#.#..\n..######..\n\n" )
elif ( c == "R" ) :
print ( "..######..\n..#....#..\n..#.##..." , end= " " )
print ( "\n..#...#...\n..#....#..\n\n" )
elif ( c == "S" ) :
print ( "..######..\n..#.......\n..######.." , end= " " )
print ( "\n.......#..\n..######..\n\n" )
elif ( c == "T" ) :
print ( "..######..\n....##....\n....##...." , end= " " )
print ( "\n....##....\n....##....\n\n" )
elif ( c == "U" ) :
print ( "..#....#..\n..#....#..\n..#....#.." , end= " " )
print ( "\n..#....#..\n..######..\n\n" )
elif ( c == "V" ) :
print ( "..#....#..\n..#....#..\n..#....#.." , end= " " )
print ( "\n...#..#...\n....##....\n\n" )
elif ( c == "W" ) :
print ( "..#....#..\n..#....#..\n..#.##.#.." , end= " " )
print ( "\n..##..##..\n..#....#..\n\n" )
elif ( c == "X" ) :
print ( "..#....#..\n...#..#...\n....##...." , end= " " )
print ( "\n...#..#...\n..#....#..\n\n" )
elif ( c == "Y" ) :
print ( "..#....#..\n...#..#...\n....##...." , end= " " )
print ( "\n....##....\n....##....\n\n" )
elif ( c == "Z" ) :
print ( "..######..\n......#...\n.....#...." , end= " " )
print ( "\n....#.....\n..######..\n\n" )
elif ( c == " " ) :
print ( "..........\n..........\n.........." , end= " " )
print ( "\n..........\n\n" )
elif ( c == "." ) :
print ( "----..----\n\n" )