常用实现
九九乘法表
1 2 3 4
| for i in range(1, 10): for j in range(1, i+1): print('{}x{}={}\t'.format(j, i, i*j), end='') print()
|
列出目录文件
1 2 3
| path = '~/path' for i in os.listdir(path): print(os.path.join(path,i))
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| def txt_processing(folder_path): folder_list = os.listdir(folder_path)
for folder in folder_list: new_folder_path = os.path.join(folder_path,folder) files = os.listdir(new_folder_path)
for file in files: if not os.path.isdir(file): TxtPath = os.path.join(new_folder_path, file) with open(TxtPath,'r',encoding='UTF-8') as fp: Txt = fp.read()
|
1 2 3 4
| for i in os.listdir(filepath): path = os.path.join(fillepath,i) if path.endswith('.xxx'): print(path)
|
判断奇偶数
1 2 3 4 5
| number = int(input('输入数字:')) if (num % 2 ) == 0: print('{}:是偶数'.format(number)) else: print('{}:是奇数'.format(number))
|
字符串判断
1 2 3 4 5 6 7 8
| str = "runoob.com" print(str.isalnum()) print(str.isalpha()) print(str.isdigit()) print(str.islower()) print(str.isupper()) print(str.istitle()) print(str.isspace())
|
字符串到小写转换
str = 'vitan.me'
# 把所有字符中的小写字母转换成大写字母
print(str.upper())
# 把所有字符中的大写字母转换成小写字母
print(str.lower())
# 把第一个字母转化为大写字母,其余小写
print(str.capitalize())
# 把每个单词的第一个字母转化为大写,其余小写
print(str.title())