輸入內容
def reverse(text):
return text[::-1]
def is_palindrome(text):
return text == reverse(text)
something = input('Enter text: ')
if is_palindrome(something):
print('Yes ,it is a palindrome')
else:
print('No,it is not a palindrome')
文件操作
poem = '''\
Programing is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = open('poem.txt','w')
f.write(poem)
f.close() #write file
f = open('poem.txt')
while True:
line = f.readline()
if len(line) == 0:
break
print(line,end=' ')
f.close()
Pickle 模塊庫使用import pickle
shoplistfile = 'shoplist.data'
shoplist = ['apple','mango','carrot']
f = open(shoplistfile,'wb')
pickle.dump(shoplist,f)
f.close()
del shoplist
f = open(shoplistfile,'rb')
storedlist = pickle.load(f)
print(storedlist)
字符編碼
# encoding=utf-8
import io
f = io.open('abc.txt','wt',encoding='utf-8')
f.write(u'Imagin non-Englisj language here')
f.close()
text = io.open('abc.txt',encoding='utf-8').read()
print(text)