美文网首页
day3-homework

day3-homework

作者: rzlong | 来源:发表于2018-08-22 20:08 被阅读0次
    • 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello Eric, would you like to learn some Python today?”。
    username = 'ranzhao'
    print("Hello "+username+", would you like to learn some Python today?")
    print("Hello %s, would you like to learn some Python today?" % username)
    
    result:
    Hello ranzhao, would you like to learn some Python today?
    Hello ranzhao, would you like to learn some Python today?
    
    • 2-4 调整名字的大小写: 将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。
    username = 'lucian'
    # 首字母大写显示
    print(username.capitalize())
    # 小写显示
    print(username.lower())
    # 大写显示
    print(username.upper())
    
    result:
    Lucian
    lucian
    LUCIAN
    
    • 2-5 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):Albert Einstein once said, “A person who never made a mistake never tried anything new.”
    # this sentence is created by rzlong
    print('Rzlong said,"Don\'t hesitate! Just do it now."')
    
    result:
    Rzlong said,"Don't hesitate! Just do it now."
    
    • 2-6 名言2: 重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息。
    famous_person = "Rzlong"
    message = 'Rzlong said,"Don\'t hesitate! Just do it now."'
    print(message)
    
    result:
    Rzlong said,"Don't hesitate! Just do it now."
    
    • 2-7 剔除人名中的空白: 存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。
    badname = '*****S\thiting\n King*****'
    badname2 = '     S\thiting\n King     '
    print(badname)
    print('------------------------')
    # lstrip()函数,剔除字符串左边的空白,也可以括号里带参数(剔除对应的字符)
    print(badname.lstrip('*'))
    # rstrip()函数剔除字符串右边的空白,也可以括号里带参数(剔除对应的字符)
    print(badname.rstrip('*'))
    # strip()函数剔除字符串两边的空白,也可以括号里带参数(剔除对应的字符)
    print(badname.strip('*'))
    print('------------------------')
    print(badname2.lstrip())
    print(badname2.rstrip())
    print(badname2.strip())
    
    reslut:
    *****S  hiting
     King*****
    ------------------------
    S   hiting
     King*****
    *****S  hiting
     King
    S   hiting
     King
    ------------------------
    S   hiting
     King     
         S  hiting
     King
    S   hiting
     King
    

    相关文章

      网友评论

          本文标题:day3-homework

          本文链接:https://www.haomeiwen.com/subject/ppzyiftx.html