2-3 個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息。顯示的消息應(yīng)非常簡(jiǎn)單,如“Hello Eric, would you like to learn some Python today?”。
name1 ='vinci'
print('Hello %s,would you like to learn some Python today?'%(name1))
#print('Hello '+name1+',would you like to learn some Python today?')
#print可以用逗號(hào)將多個(gè)內(nèi)容同時(shí)打印,但多個(gè)內(nèi)容用空格隔開
print('Hello ',name1',would you like to learn some Python today?')
結(jié)果顯示

2.3
2-4 調(diào)整名字的大小寫: 將一個(gè)人名存儲(chǔ)到一個(gè)變量中,再以小寫、大寫和首字母大寫的方式顯示這個(gè)人名。
name1 = 'Vinci da'
print(name1.upper())
print(name1.lower())
print(name1.title())
結(jié)果顯示

2.4
2-5 名言: 找一句你欽佩的名人說的名言,將這個(gè)名人的姓名和他的名言打印出來。輸出應(yīng)類似于下面這樣(包括引號(hào)):Albert Einstein once said, “A person who never made a mistake never tried anything new.”
name1 = 'Tesia'
word = 'Common sense is the collection of prejudices acquired by age eighteen.'
print("%s once said,\"%s\""%(name1,word))
# print('%s once said,"%s"'%(name1,word))另一種方法
結(jié)果顯示

2.5
2-6 名言2: 重復(fù)練習(xí)2-5,但將名人的姓名存儲(chǔ)在變量famous_person 中,再創(chuàng)建要顯示的消息,并將其存儲(chǔ)在變量message 中,然后打印這條消息。
famous_person='Tesia'
message='Common sense is the collection of prejudices acquired by age eighteen.'
print(famous_person+' once said,'+'\"'+message+'\"')
#print(famous_person+' once said,'+'"'+message+'"') 另一種方法
結(jié)果輸出

2.6
2-7 剔除人名中的空白: 存儲(chǔ)一個(gè)人名,并在其開頭和末尾都包含一些空白字符。務(wù)必至少使用字符組合"\t" 和"\n" 各一次。 打印這個(gè)人名,以顯示其開頭和末尾的空白。然后,分別使用剔除函數(shù)lstrip() 、rstrip() 和strip() 對(duì)人名進(jìn)行處理,并將結(jié)果打印出來。
famous_name = '\t\tVinci da\n\n'
print(famous_name)
print(famous_name.lstrip())
print(famous_name.rstrip())
print(famous_name.strip())
結(jié)果輸出

2.7