![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python面试100讲
Python面试100讲
H Journey
这个作者很懒,什么都没留下…
展开
-
print函数用法:指定输出分隔符、多次输出不换行、%格式化输出字符串
'''用指定字符分割输出字符串'''print('aa','bb',sep='==')'''如何不换行输出'''print('hello',end=' ')print('world')'''格式化输出数据'''data = 'data'length = len(data)print('the length of %s is %d'%(data,length))#将输出赋值给变量from io import StringIOimport sysold_stdout原创 2021-01-31 11:14:32 · 1274 阅读 · 0 评论 -
Python转义字符、原始格式字符串
'''同时在字符串中显示单引号和双引号'''print("hello 'world'")#hello 'world'print('"hello" world')#"hello" worldprint("'hello' \"world\"")#'hello' "world"'''让转义符失效的方法:r、repr、\\'''print(r'hello \n \'world')#hello \n \'worldprint(repr('\\nhello\world'))#'\\nhello\原创 2021-01-30 19:40:14 · 315 阅读 · 0 评论 -
格式化整数和浮点数
'''整数格式化格式化一个整数,按10位输出,不足10位前面补0'''x1 = 1234print(format(x1,'*>10d'))#******1234print(format(x1,'*<10d'))#1234******print(format(x1,'*^10d'))#***1234***'''浮点数格式化格式化一个浮点数,保留两位小数'''x1 = 1234.56789x2 = 30.1print(format(x1,'0.2f'))#1234.56原创 2021-01-30 18:32:45 · 379 阅读 · 0 评论 -
如何反转一个字符串
'''如何反转一个字符串'''s1 = 'abcdef's2 = ''for c in s1: s2 = c + s2print(s2)'''如何用分片反转字符串'''print(s1[::-1])#将步长设为-1原创 2021-01-30 18:14:23 · 88 阅读 · 0 评论 -
如何检测一个字符串是否为数字
'''如何检测字符串是否位数字、数字和字母混合形式'''#检测字符串是否由字母或数字组成 isalnum'''Return True if the string is an alpha-numeric string, False otherwise.A string is alpha-numeric if all characters in the string are alpha-numeric and there is at least one character in the strin原创 2021-01-30 17:59:33 · 381 阅读 · 0 评论 -
改变字符串首字母的大小写
'''字符串首字母大小写转换'''s = 'hello'print(s.capitalize())s1 = 'H' + s[1:]print(s1)s1 = s[0].upper() + s[1].lower() + s[2:]print(s1)#s[0] = 'H' 只读的,会抛出异常'''拆分字符串,并将每个单词首字母变成大写'''s = 'hello world'arr = s.split(' ')new_str = f'{arr[0].capitalize(原创 2021-01-30 16:34:45 · 204 阅读 · 0 评论 -
十进制、二进制、八进制、十六进制之间的转换
'''如何表示不同进制的数值'''n10 = 123 #十进制n2 = 0b101011101 #二进制n8 = 0o12472 #八进制n16 = 0xF12 #十六进制print(n10)print(n2)print(n8)print(n16,'\n')'''进制之间的转换'''#二进制、八进制、十六进制转为十进制print(int('10010', 2))print(int('0o2574',8))print(int原创 2021-01-30 15:38:36 · 324 阅读 · 0 评论 -
不同数类型的变量或值如何连接
'''字符串与字符串之间如何连接,有几种方式'''#第1种 使用 + (加号)s1 = 'hello's2 = 'world's = s1 + s2print(s+'\n')#第2种 直接连接s = 'hello' 'world''\n'print(s)#第3种 使用逗号(,)连接,标准输出的重定向print('hello','world','\n')from io import StringIOimport sysold_stdout = sys.stdoutresul原创 2021-01-30 15:07:52 · 250 阅读 · 0 评论 -
Python 路径设置
#1:设置python模块的搜索路径有几种方式'''1、设置PYTHONPATH环境变量2、通过添加.pth文件3、通过sys.path设置路径'''#2:永久设置python模块的搜索路径有几种方式,如何使用他们'''1、在python安装路径下添加*.pth文件,在文件中添加需要的路径'''#3:如何临时设置python模块的搜索路径import syssys.path.append('.\\modules')import working_funprint(work原创 2021-01-30 14:10:32 · 2547 阅读 · 0 评论 -
导入python模块的方式
#导入python模块的方式#在Python语言中,可以使用import导入模块中的所有成员,#也可以使用from...import导入模块中所有成员或者指定成员。#当使用as为模块或模块中的成员指定别名时,一旦指定了别名,原来的名字就不能用了#1:直接导入模块import mathprint(math.sin(213))#2:从模块导入指定类、方法、成员等from math import cos,tanprint(cos(123))print(tan(123))#3:导入模块原创 2021-01-30 11:06:41 · 132 阅读 · 0 评论