Try   HackMD

影片大綱

  1. 算術運算:加減乘除、取餘數%、小數除法/、整數除法//、開次方**。
  2. 字串的表示法:單行、多行、跳脫字元。
  3. 字串的串接與重複。
  4. 字串中的字元與子字串:編號與索引操作。

影片

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


筆記

  • 數字: 加、減、乘、除、取餘數
    • 除法: 整數除法//、小數除法/
    • X的Y次方: X**Y
  • 字串
    • 表示法詳解: 雙引號、單引號、多行文字
    • 重覆與串接
    • 索引與字元: 字串中的字元或是子字串。

YT 留言

# 數字運算
x=3+6
print(x)

x=3-6
print(x)

x=3*6
print(x)

x=3/6
print(x)

x=3//6
print(x)

x=3/7
print(x)

x=3//7
print(x)

x=7/6
print(x)

x=7//6
print(x)

x=2**3
print(x)

x=2**0.5
print(x)

x=7%3
print(x)

x=2+3
print(x)
x+=1 # x=x+1 # 將變數中數字 +1
print(x)
x-=1 # x=x-1 # 將變數中數字 -1
print(x)

# 字串運算
s="Hello"
print(s)

s='Hello'
print(s)

s="Hell\"o"
print(s)

s="Hello"+"World"
print(s)

s="Hello\nWorld"
print(s)

s="""Hello


World"""
print(s)

s="Hello"*3+"World"
print(s)

# 字串會對內部的字元編號(索引),0 開始算起
s="Hello"
print(s[0])
print(s[1])
print(s[2])
print(s[1:4])
print(s[:4])
print(s[1:])
# 數字運算
#   x = 2+3
#   print(x)
#   x//=1 # x = x+1 # 將變數中的字 +1
#   print(x)

# 字串運算
#   s = "Hell\"o"  # \ = 跳脫 = Hell"o
#   print(s)
#   s = "hello" "world"
#   print(s)
#   s = "Hello\nworld"
#   print(s)
#   s = "Hello"*3+"world"
#   print(s)
# 字串會對內部的字元編號(索引),從 0 開始算起
s = "hello"
print(s[1:4])   # 包含開頭不包含結尾,得 ell
s = "hello"
print(s[1:])    # 包含開頭的全部到結尾,得 ello
s = "hello"
print(s[:4])    # 開頭的全部到結尾前一個,得 hell
請問如果我要把“Hello"*3的那三個Hello分開或換行的話該怎麼做?

---
換行可以用換行符號:"Hello\n"*2+"Hello"
分開看你要用什麼符號,例如用逗號:"Hello,"*2+"Hello"
老師好,想請問如何提取字串中的第0,2,4個字元呢?
若寫成print(s[0,2,4])時會顯示錯誤
感謝

---
就分開寫嘍 ~~~
print(s[0]+s[2]+s[4])
x = 'Hello, World'
x[-2] # l
x[4:-2] # o, Wor
x[4:-4] # o, W
請問老師input用法

---
input(提示字串),會以字串的型態回傳使用者的輸入。
# Section1.數字運算
x = 2*3
y = 2**3 # power
z = 2**0.5 # Square root
u = 5%1

x = x + 1
x += 1
# x++ # Noooo!

# Section2.字串運算
str = "Hell\"o "
str2 = "Hell\"o " + 'world'
str2 = "Hell\"o " 'world' # 空白等於串接
str3 = """Hello


World""" # 3個引號內任意換行
str4 = 'Hello'*3 +'World'
print(str4)

# 字串會對內部的字元編號(index索引),從0開始算起
string1 = 'MISbetterthanCS'
print( string1[1] ) #[0] > M, [1] > I...
print( string1[3:] ) #從index=3到結尾的字串 > betterthanCS
print( string1[:3] ) #從開頭到index=3的字串 > MIS
请问彭彭老师
print("""
A
B
C
"""=="\nA\nB\nC\n")
答案为什么会是true呢?

---
\n 是換行符號,相當於上方使用多行字串中的換行 ~~~~
如何多行注釋?

---
你可以用多行字串的符號:連續三個單引號。
'''
註解
很多行
'''
只要不放進變數裡或做其他使用,Python 的引擎會忽略它。

END