[Python初學者] 8種轉義字符 ( Escape Characters ) + 7種基礎運算字符( Basic operators )

Escape Characters 轉義字符

No.
Code
Name
Escape Characters

1

\’

單引號 ( Single Quote )

 ‘

2

\\ 反斜線 ( Backslash ) \

3

\n 換行 ( New Line ) 換行

4

\t 空格 ( Tab ) 空格

5

\b 退格鍵 ( Backspace ) 去掉空格

6

\ooo 八進制值 ( Octal value )  

7

\xhh 十六進制值 ( Hex value )  

8

\f 換頁 ( Form Feed )  

Basic operators 基礎運算字符

No.
Operator
Name
Example

1

+

加 ( Addition ) 3 + 5 == 8

2

減( Subtraction ) 6 3 == 3

3

*

乘( Multiplication ) 8 * 2 == 16

4

/

除( Division ) 5 / 2 == 2.5

5

%

相除之後的餘數( Modulus ) 8 % 3 == 2
8 % 2 == 0

6

//

相除之後無條件捨去( Floor division ) 10 // 3 == 3

7

**

指數( Exponential ) 2 ** 3 == 8

1.單引號

txt = 'C\'est bon.'
print(txt)
C'est bon.

 

2. 反斜線 ( Backslash )

txt = "\\ Coucou \\."
print(txt)
\ Coucou \.

 

3. 換行 ( New Line )

txt = "Bonjour\nOceane!"
print(txt)
Bonjour
Oceane!

 

4. 空格 ( Tab )

txt = "Bonjour\tOceane!"
print(txt)
Bonjour	Oceane!

 

5. 退格鍵 ( Backspace )

txt = "Bonjour \bOceane!"
print(txt)
Bonjour Oceane!

 

6. 八進制值 ( Octal value )

txt = "\102\157\156\152\157\165\162"
print(txt)
Bonjour

 

7. 十六進制值 ( Hex value ):輸入 Hex value 需要加上 x

txt = "\x42\x6F\x6E\x6A\x6F\x75\x72"
print(txt)
Bonjour

 

註:Octal value 和 Hex value 對照表:https://www.sciencebuddies.org/science-fair-projects/references/ascii-table