4.字符串(2-6/2-7)

2-6 名言 2:重复练习 2-5,但将名人的姓名存储在变量 famous_person 中,再创建
要显示的消息,并将其存储在变量 message 中,然后打印这条消息。

famous_person = 'Albert Einstein'
message = 'A person who never made a mistake never tried anything new.'
print(famous_person +" once said,\"" +message +"\"." )

要特别注意\" 转义字符

F5 运行

Albert Einstein once said, “A person who never made a mistake never tried anything new.” 

2-7 剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。
务必至少使用字符组合"\t"和"\n"各一次。
打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数 lstrip()、
rstrip()和 strip()对人名进行处理,并将结果打印出来。

 

test_name = ' Li xiao long '
print("Name is:\n" + test_name)
print("\t"+test_name)
print(test_name.strip())
print(test_name.rstrip())
print(test_name.lstrip())

F5运行:

Name is:

 Li xiao long

            Li xiao long

Li xiao long

 Li xiao long

Li xiao long

 

posted @ 2018-10-16 22:23  宝镖  阅读(201)  评论(0)    收藏  举报