s = "I'm a string"
t = 'I said "This is a string".'
chars = input()
c = 0
for ch in chars:
if ch in s:
c += 1
for i in range(len(t)):
if t[i] in s:
c += 1
r = ""
for k in range(2,10,2):
r += str(k) # 2468
r = 2*r # 24682468
x = input().split(", ")
out = []
k = 0
for e in x:
out.append((s+" "*12)[:12]
k += 1
if k % 3 == 0:
print("".join(out))
out = []
print("".join(out))
Affogato Americano Cappuccino
Espresso Latte Macchiato
Mocha Ristretto
2110101 วิศวกรรมคอมพิวเตอร์ จุฬาฯ (14/09/67)
5
import urllib.request
def find(s, start, c):
for i in range(start, len(s)):
if s[i] == c: return i
return -1
url = "http://air4thai.pcd.go.th/services/getNewAQI_XML.php?stationID=52t"
web = urllib.request.urlopen(url)
for line in web:
line = line.decode()
if "<PM25 value=" in line:
i = find(line, 0, '"')
j = find(line, i+1, '"')
print("PM 2.5 =", line[i:j])
break
2110101 วิศวกรรมคอมพิวเตอร์ จุฬาฯ (14/09/67)
6
อักขระพิเศษ (Escape Characters)
s = "Hello"
print(s) # Hello
s = "\"Hello\""
print(s) # "Hello"
s = "\'Hello\'"
print(s) # 'Hello'
s = "\"\'\\Hello\\"
print(s) # "'\Hello\
s = "Hello\nPython" # Hello
# Python
\"
คือ
"
\'
คือ
'
\\
คือ
\
\n
คือ รหัสให้ขึ้นบรรทัดใหม่
2110101 วิศวกรรมคอมพิวเตอร์ จุฬาฯ (14/09/67)
7
ตัวอย่าง: แทน " ' / \ ( ) , . : ; ด้วยช่องว่าง
def blank(t):
result = ""
for c in t:
if c in "\"\'/\\,.:;":
result += " "
else:
result += c
return result
import urllib.request
url = "http://air4thai.pcd.go.th/services/getNewAQI_XML.php?stationID=52t"
web = urllib.request.urlopen(url)
for line in web:
line = line.decode()
pattern = '<PM25 value="'
i = line.find(pattern)
if i >= 0:
i += len(pattern)
j = line.find('"', i)
print("PM 2.5 =", line[i:j])
break
2110101 วิศวกรรมคอมพิวเตอร์ จุฬาฯ (14/09/67)
12
def split(t, s):
x = []
k0 = 0
k = t.find(s,0)
while k >= 0:
x.append(t[k0:k])
k0 = k+1
k = t.find(s,k0)
x.append(t[k0:])
return x