2-1 Simple message : Store a message in a variable and print it out.
1 message="Hello,world!" 2 print(message)
output:
Hello,world!
2-2 Multiple simple messages: store a message in a variable and print it out; then modify the value of the variable to a new message and print it out.
1 message="Hello,world!" 2 print(message) 3 message="Hello,python world!" 4 print(message)
output:
1
2
|
Hello,world!
Hello,python world!
|
2-3 Personalized message: Store the user's name in a variable and display a message to that user. The displayed message should be as simple as "Hello Eric, would you like to learn some Python today?".
1 name='Eric' 2 message="Hello "+name+",would you like to learn some Python today?" 3 print(message)
output:
1 Hello Eric,would you like to learn some Python today?
2-4 Adjust the case of the name: Store a person's name in a variable, and then display the person's name in lowercase, uppercase, and initial capital.
1 name= ' Eric ' 2 print (name.lower()) #Each letter is lowercase 3 print (name.upper()) #Each letter is uppercase 4 print (name.title()) #First letter is uppercase
output:
1 eric 2 ERIC 3 Eric
2-5 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号): Albert Einstein once said, “A person who never made a mistake never tried anything new.”
1 message='Albert Einstein once said,"A person who never made a mistake never tried anything new."' 2 print(message)
输出:
Albert Einstein once said,"A person who never made a mistake never tried anything new."
2-6 名言2:重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息。
1 famous_person='Albert Einstein' 2 message=famous_person+' once said,"A person who never made a mistake never tried anything new."' 3 print(message)
输出:
1 Albert Einstein once said,"A person who never made a mistake never tried anything new."
2-7 剔除人名中的空白: 存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。
1 name=' Eric ' #两边分别空两个空格 2 print(name) 3 print("\t"+name) #'\t'制表符 4 print("\n"+name) #'\n'换行符 5 print(name.rstrip()) #删除右边空白 6 print(name.lstrip()) #删除左边空白 7 print(name.strip()) #删除两边空白
输出:
1 Eric 2 3 Eric 4 Eric 5 Eric 6 Eric
2-8 数字 数字8: 编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8。为使用print 语句来显示结果,务必将这些表达式用括号括起来,也 就是说,你应该编写4行类似于下面的代码:输出应为4行,其中每行都只包含数字8。
1 print(5+3) 2 print(10-2) 3 print(2*4) 4 print(16/2) 5 print(2**3) #2的3次方
输出:
1 8 2 8 3 8 4 8.0 5 8
2-9 最喜欢的数字: 将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来。
1 num='8' 2 message="My favorite number is "+num+' .' 3 print(message)
输出:
My favorite number is 8 .