编程语言
首页 > 编程语言> > python-小练习(1)

python-小练习(1)

作者:互联网

个性化消息:将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello Eric, would you like to learn some

Python today?”。

name = "Zxx"
# 方法一
print("Hello"" " + name + ",would you like to learn some Python today?")
# 方法二
print("Hello"" " "%s,would you like to learn some Python today?" % (name))
# 方法三
print("Hello" " ""{0},would you like to learn some Python today?".format(name))
# 方法四
print(f"Hello {name},would you like to learn some Python today?")

调整名字的大小写:将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。

name = "zyy"
print(name.upper())
print(name.lower())
print(name.title())

名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言

打印出来。输出应类似于下面这样(包括引号):

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


iname = "Albert Einstein on"
said_message = '"A person who never made a mistake never tried anything new."'

print(f"{iname}said,{said_message}")

执行结果

C:\Users\yong.chen.cd\PycharmProjects\pythonProject1\venv\Scripts\python.exe C:/Users/yong.chen.cd/PycharmProjects/pythonProject1/study_python/personalise_name.py
Hello Zxx,would you like to learn some Python today?
Hello Zxx,would you like to learn some Python today?
Hello Zxx,would you like to learn some Python today?
Hello Zxx,would you like to learn some Python today?
ZYY
zyy
Zyy
Albert Einstein onsaid,"A person who never made a mistake never tried anything new."

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

```python
famous_person="Albert Einstein on"
message='"A person who never made a mistake never tried anything new."'
print(f"{famous_person}said,{message}")

2-7 剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t"和"\n"各一次。

打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip()、rstrip()和strip()对人名进行处理,并将结果打印出来。

name1="ZYY "
print(name1)
name2="\n\t"+name1.rstrip()+"\t"
print(name2)
print(name2.lstrip())
print(name2.rstrip())
print(name2.strip())

执行结果

Albert Einstein onsaid,"A person who never made a mistake never tried anything new."
ZYY 

	ZYY	
ZYY	

	ZYY
ZYY

进程已结束,退出代码为 0

标签:never,name,python,练习,some,learn,print,like
来源: https://www.cnblogs.com/cy-xt/p/16076474.html