从零开始学习python:demo2.3
字符串拼接+:
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)
作業(yè):
-------------------------------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------------------------------
?
轉載于:https://www.cnblogs.com/sandy32399/p/10535942.html
總結
以上是生活随笔為你收集整理的从零开始学习python:demo2.3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 豁免保费是什么意思
- 下一篇: 来分期公司地址在哪里