Day1~Day3在我的方格子 : https://vocus.cc/user/6581c332fd897800011ce225
python沒有random()這個方程式
所以要在一開始import進去
import random
print(random.randrange(1, 100))
把資料轉成整數。
可以把整數、浮點數(會捨去小數點)、或「表示整數的字串」轉成 int。
把資料轉成浮點數。
可以處理整數、浮點數、或「表示數字的字串」。
把資料轉成字串。
幾乎什麼型態都能轉成字串。
EX :
字串可以用單引號 '...' 或**雙引號 "..." **包起來。
print("Today is hot !") # 字串外面是雙引號,裡面用單引號沒問題
print("She is called 'Debby'") # 外面雙引號,裡面單引號沒問題
print('She is called "Debby"') # 外面單引號,裡面雙引號沒問題
output :
Today is hot !
She is called 'Debby'
She is called "Debby"
補充:如果內外都是同一種引號怎麼辦?
你可以用跳脫字元(escape character)\ 來避免語法錯誤
簡單來說就是用\保護單引號'或是雙引號"
EX :
print('It\'s hot') # 單引號內還是要用單引號時,加 \
print("She is called \"Debby\"") # 雙引號內再用雙引號時,加 \
output :
It's hot
She is called "Debby"
用法
如果要讓字串跨越多行,可以用三個雙引號 """...""" 或三個單引號 '''...''' 把內容包起來。
a = "Hi Debby"
print(a[1]) # 輸出: i (第二個字元)
output :
i
可以用 for 迴圈逐字元走訪字串:
for x in "banana":
print(x)
# 會依序印出 b a n a n a
這裡 "banana" 是一個字串(string),而字串本質上是「字元的序列」。
for x in [0,1,2,3,4,5]:
print(x)
#會依序印出0 1 2 3 4 5
這裡 [0, 1, 2, 3, 4, 5] 是一個「串列」(list),裡面有六個數字。
for x in range(6):
print(x)
# 會依序印出 0 1 2 3 4 5
用 len() 取得字串長度(字元數)。
a = "I'm learning Python!"
print(len(a)) # 輸出: 20
txt = "The best things in life are seafood!"
print("seafood" in txt)
if "seafood" in txt:
print("Yes, 'seafood' is present.")
print("expensive" not in txt)
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
output :
True
Yes, 'seafood' is present.
True
No, 'expensive' is NOT present.
今天先醬~要來看下禮拜的期末考了~
https://vocus.cc/article/68428090fd89780001661c90