最近在学learn python the hard way,学习到第37章,进行了关于关键字.转义符和字符串格式化的总结.看手头上的中文版没有及时更新.于是就把这些翻译过来,以作查阅. 关键字: 关键字 描述 例子 and 逻辑与 True and False == False as 作为with-as语句的一部分 with X as Y: pass assert 保证某些事情为真 assert False, "Error!" break 马上停止循环 while True: brea
# 格式化字符串: 在字符串前加上 f 或者 F 使用 {变量名} 的形式来使用变量名的值 year = 2020 event = 'Referendum' value = f'Results of the {year} {event}' print(f'Results of the {year} {event} \n', value) # : .3f 标示 保留前面的变量值/字面值3位小数 , 3d, 3 则是让盖子段成为最小字符宽度,在使列对齐时作用大 print(f'The value o
部分转自:https://www.cnblogs.com/hellofengying/p/10183057.html 今天再打开文件名时,出现了错误,如下: In []: path='D:\Code\PythonWorkPlace\PythonProject\pydata-book-2nd-edition\data sets\bitly_usagov\example.txt' In []: open(path).readline() -------------------------------
1)%格式化方法 >>> a = "this is %s %s" % ("my", "apple") >>> a 'this is my apple' 2)内置方法format(): >>> a = "this is {} {}".format("apple","my") >>> a 'this is apple m