2-3 個性化消息: 將用戶的姓名存到一個變量中,并向該用戶顯示一條消息死讹。顯示的消息應非常簡單,如“Hello Eric, would you like to learn some Python today?”瘪板。
name = "Eric"
print("Hello %s,would you like to learn some Python today?"%name)
'''
結(jié)果:
Hello Eric,would you like to learn some Python today?
'''
2-4 調(diào)整名字的大小寫: 將一個人名存儲到一個變量中乃秀,再以小寫、大寫和首字母大寫的方式顯示這個人名藤肢。
name = 'alice'
#顯示大寫姓名
print(name.upper())
#顯示小寫姓名
print(name.lower())
#顯示首字母小寫姓名
print(name.title())
print(name.capitalize())
'''
結(jié)果:
ALICE
alice
Alice
Alice
'''
2-5 名言: 找一句你欽佩的名人說的名言太闺,將這個名人的姓名和他的名言打印出來。輸出應類似于下面這樣(包括引號):Albert Einstein once said, “A person who never made a mistake never tried anything new.”
print("Albert Einstein once said, \"A person who never made a mistake never tried anything new.\"")
'''
結(jié)果:
Albert Einstein once said, "A person who never made a mistake never tried anything new."
'''
2-6 名言2: 重復練習2-5嘁圈,但將名人的姓名存儲在變量famous_person 中省骂,再創(chuàng)建要顯示的消息,并將其存儲在變量message 中最住,然后打印這條消息钞澳。
famous_person = "Albert Einstein"
message = "A person who never made a mistake never tried anything new."
print("%s once said:\"%s\""%(famous_person,message))
'''
結(jié)果:
Albert Einstein once said:"A person who never made a mistake never tried anything new."
'''
2-7 剔除人名中的空白: 存儲一個人名,并在其開頭和末尾都包含一些空白字符涨缚。務必至少使用字符組合"\t" 和"\n" 各一次轧粟。 打印這個人名,以顯示其開頭和末尾的空白。然后兰吟,分別使用剔除函數(shù)lstrip() 通惫、rstrip() 和strip() 對人名進行處理,并將結(jié)果打印出來混蔼。
name = " alice "
print("\n"+name+"\n")
#剔除左邊的空白
print(name.lstrip())
print(name)
print("\t"+name)
#剔除右邊的空白
print(name.rstrip()+name)
print(name+name)
#剔除全部空白
print(name.strip()+name.strip()+"\n"+name.strip()+name.strip())
'''
結(jié)果:
alice
alice
alice
alice
alice alice
alice alice
alicealice
alicealice
'''