亞洲大學基礎程式設計教材(AUP110-Fundamentals of Programming)¶
#Week7-常用內建函數以及字串的使用
Topic 1(主題1)-輸入參數的複習¶
input()
Step 1: 輸入一個字串參數¶
instr = input()
print(instr)
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
/tmp/ipykernel_918/3106458668.py in <module>
----> 1 instr = input()
2 print(instr)
~/.local/lib/python3.8/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
1001 """
1002 if not self._allow_stdin:
-> 1003 raise StdinNotImplementedError(
1004 "raw_input was called, but this frontend does not support input requests."
1005 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
Step 2: 輸入一個整數參數¶
instr = input()
val = int(instr)
print(val)
#合併輸入和整數的轉換
val = int(input())
print(val)
Step 3: 輸入一個浮點數參數¶
instr = input()
val = float(instr)
print(val)
#合併輸入和浮點數轉換
val = float(input())
print(val)
Step 4: 輸入二個字串參數¶
instr = input()
paras = instr.split()
a = paras[0]
b = paras[1]
print(f"{a} {b}")
#合併輸入和字串分割
a, b = input().split()
print(f"{a} {b}")
Step 5: 輸入三個整數參數¶
instr = input()
paras = instr.split()
a = int(paras[0])
b = int(paras[1])
c = int(paras[2])
print(f"a={a} b={b} c={c}")
#合併輸入和數字轉換
a, b, c = map(int, input().split())
print(f"a={a} b={b} c={c}")
Topic 2(主題2)-格式化輸出的複習¶
Step 1: 格式化輸出多個變數¶
year = 2016
event = 'Referendum'
print(f'Results of the {year} {event}')
Step 2: 控制格式化輸出的寛度和排列¶
yes_votes = 42_572_654
no_votes = 43_132_495
percentage = yes_votes / (yes_votes + no_votes)
print(f'{yes_votes:9} YES votes {percentage:2.2%}')
Topic 3(主題3)-字串的索引(Indexing)和切片(Slicing)¶
Step 1: 字串的索引(Indexing)¶
string.index(value, start, end)
txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x)
txt = "Hello, welcome to my world."
print(txt[7])
txt = "Hello, welcome to my world."
print(txt[7:14])
Step 2: 字串的切片(Slicing)¶
str1 = "Hello, World!"
print(str1[7:13])
str1 = "Hello, World!"
print(str1[7:])
str1 = "Hello, World!"
print(str1[-6:])
str1 = "Hello, World!"
print(str1[-6:-3])
str1 = "Hello, World!"
print(str1[::-1])
Topic 4(主題4)-字串的函數¶
分割字串 str.split()
大小寫 str.lower(),str.upper()
去頭去尾 str.strip(),str.rstrip(),str.lstrip()
Step 1: 分割 str.split()¶
original_string = "ab_cd_ef"
split_string = original_string.split("_")#splits string by "_"
print(split_string)
original_string = "ab cd ef"
split_string = original_string.split()#splits string by " "
print(split_string)
Step 2: 大小寫 str.lower(),str.upper()¶
ss = "Google was founded in September 1998 by Larry Page and Sergey Brin";
s1 = ss.lower()
s2 = ss.upper()
print(s1)
print(s2)
Step 3: 去頭去尾 str.strip(),str.rstrip(),str.lstrip()
ss = " Google was founded in September 1998 by Larry Page and Sergey Brin ";
s1 = ss.strip() #去頭尾
s2 = ss.rstrip() #去尾
s3 = ss.lstrip() #去頭
print(s1)
print(s2)
print(s3)
Topic 5(主題5)-ascii/unicode 轉換的函數¶
Step 1: ord()函數¶
ord()給定一個長度為1的字符串,當參數為unicode對象時,返回一個整數,該整數表示字符的Unicode代碼點;如果參數為8位字符串,則返回ascii的值。
# 輸入字元
ch = input("Keyin a character: ")
print( "The ascii code of character {} is {}".format(ch, ord(ch)))
Step 2: chr()函數¶
s1 = "大家好"
s2 = "\u5927\u5bb6\u597d"
print(s1)
print(s2)
s1 = "大家好"
s2 = "\u5927\u5bb6\u597d"
print("The unicode code of characters {} is {:d} {:d} {:d}".format(s1, ord(s1[0]), ord(s1[1]), ord(s1[2])))
s1 = "大家好"
s2 = "\u5927\u5bb6\u597d"
print("The unicode code of characters {} is \\u{:x} \\u{:x} \\u{:x}".format(s1, ord(s1[0]), ord(s1[1]), ord(s1[2])))
# 輸入字元
ch = input("Keyin a character: ")
print( "The unicode code of character {} is {:d}".format(ch, ord(ch))) #:d 10進位數
print( "The unicode code of character {} is \\u{:x}".format(ch, ord(ch))) #:x 16進位數
code = 23478
print(f"The unicode code of code {code} is {chr(code)}") # 10進位數
code = "23478"
code = int(code)
print(f"The unicode code of code {code} is {chr(code)}") # 10進位數
code = "597d"# 16進位數
code = int(code,16)# 16進位數轉換
print(f"The unicode code of code {code:x} is {chr(code)}") # 16進位數