从零开始学习python:demo2.3

时间:2021-02-03 01:52:14

字符串拼接+:

first="hello" #将hello赋值给变量first
second="world" #将world赋值给变量second
full=first+“ ”+second #拼接first 空格 second并复制给full
print(full) #打印full完整的值
--------------------------

hello world

--------------------------

示例1:

first="hello"
second="world"
full=first+" "+second
print(full)
print("Baby,"+full.title())

拼接后都存储在一个变量中:

first="hello"

second="world"

full=first+" "+second

msg="Baby,"+full.title()+"!"

print(msg)

-------------------------------------

制表符\t:不使用表格的情况下在垂直方向按列对齐文本。

换行符\n

示例:

print("Hello")
print("\tHello")
print("HelloWorld")
print("Hello\nWorld")
print("Lauguages:\n\tPython\n\tJAVA\n\tC++\n\tRuby")

------------------------------------------------------------------

Hello
    Hello
HelloWorld
Hello
World
Lauguages:
    Python
    JAVA
    C++
    Ruby

------------------------------------------------------------------

删除字符串末尾空白方法:rstrip()

删除字符串开头空白方法:lstrip()

同时删除字符串开头末尾空白方法:trip()

示例

first=input("请输入你的名字:")
first=first.rstrip() #直接调用的话只能暂时删除空白,下一次访问first的值时依然会有空白,要永久删除这个字符的空符号,必须将删除操作的结果再存回到变量中

first=first.lstrip()

first=first.strip()

print(first)

------------------------------------------------------------------

name=input("请输入你的名字:")
msg="hello"+" "+name.title()+","+"would you like to learn some Python today?"
print(msg)

作业:

-------------------------------------------------------------------------------------------------------

name = input("请输入你的名字:")
msg = "hello"+" "+name.title()+","+"would you like to learn some Python today?"
print(msg)
name = name.title()
print(name)
name = name.upper()
print(name)
name = name.lower()
print(name)

-------------------------------------------------------------------------------------------------------

请输入你的名字:sandy shirley
hello Sandy Shirley,would you like to learn some Python today?
Sandy Shirley
SANDY SHIRLEY
sandy shirley

-------------------------------------------------------------------------------------------------------