python面试100讲 第三章(字符串与正则表达式)

第三章:Python面试必考重点:字符串与正则表达式
#第一题:1.字符串格式化的方式
2.模板字符串
面试题1:在python语言中有多少中格式化字符串的方法
面试题2:请解释什么是模板字符串,如何使用

面试题1:
(1)%格式化
(2)模板字符串
(3)字符串的format方法
(4)fstring

面试题2:
通过Template对象封装, $放置一些占位符,并通过substitute方法用实际的值替换这些占位符。


from string import Template

template1 = Template('$s是我最喜欢的,$s很帅')
print(template1.substitute(s = '吴某'))

#如果$后面的名字和英文混合,怎样区分:如果$后面有对{},则将里面的认为为占位符.
template2 = Template('${h}ello world')
print(template2.substitute(h = 'abc'))


#如果要输出$,则使用两个$,即可。
#如:
template3 = Template('$dollar$$相当于$pounds英镑')
#可以把要传入的值放入字典直接传入字典
date = {}
date['dollar'] = 30
date['pounds'] = 25
print(template3.substitute(date))

######################################################
第二题:用fstring方法格式化字符串
面试题1:在python语言中哪种格式化方式可以直接使用变量
面试题2:请用代码描述如何使用fstring格式化字符串

面试题1:
fstring方式

面试题2:
(1)

#可以直接使用变量
name = 'Bill'
age = 20

#可以直接使用函数
def getAge():
	return 21

s = f"我是{name},我今年{age}岁,明年{getAge()岁"

print(s)

(2)

#可以直接使用对象
class Person:
	def __init__(self):
		self.name = 'Mike'
		self.age1 = 40

	def getAge(self):
		return 41

person = Person()

s = f"我是{person.name},我今年{person.age}岁,明年{person.getAge()岁"

######################################################
第三题:1.字符串的分片
2.字符串的乘法
3.判断字符在字符串中是否存在
4.字符串的长度
5.字符串中按ASCII值算最大的字符和最小的字符
面试题1:请详细描述python字符串支持的基本操作

#(1)通过索引获取字符串中的某个字符

s1 = 'hello world'
print(s1[0])

#(2)分片

print(s1[6,9])

#(3)乘法

s2 = 'xyz'
print(s2 * 10)

#(4)判断字符在字符串中是否存在

print('b' in s2)

#(5)

print(len(s2))    
print(min(s2))
print(max(s2))

######################################################
第四题:format格式化字符串的详细用法
面试题1:字符串的format方法有几种指定参数的方式
面试题2:请详细描述字符串的format方法如何格式化字符串

面试题1:
(1)默认方式(传入的参数与{}一一对应

(2)命名参数

(3)未知参数{2}

面试题2:
(1)

s1 = 'Today is {},the temperature is {} degrees.'
print(s1.format('Saturday',24))

(2)

s2 = 'Today is {day},the temperature is {degree} degrees.'
print(s2.format(degree = 30,day = 'Saturday'))

(3)

s4 = Today is {week},{1},the {0} temperature is {degree} degree.'

print(s4.format('abds',1234,degree = 45,week = 'Sunday'))

#如果是对象呢:
class Person:
	def __init__(self):
		self.age = 12
		self.name - 'Bill'
person = Person()

s5 = "My name is {p.name},my age is {p.age}."
print(s5.format(p = person))

######################################################
第五题:学会让字符串居中显示的各种方法
面试题1:如何让字符串居中显示,有哪些方法
面试题2:请使用center方法让字符串居中显示,两侧显示#号

面试题1:
(1)center方法

print('<' + 'hello'.center(30) + '>')       #输出为:<            hello             >
print('<' + 'hello'.center(30,'#') + '>')	#输出为:<############hello#############>

(2)format方法 和上面显示一样

print('<{:^30}'.format('hello'))
print('<{:#^30}'.format('hello'))

#########################################################
第六题:join方法的应用
面试题1:如何将列表中的元素(字符串类型的值)连接在一起(首尾相接)
面试题2:字符串的join方法的作用是什么,使用join应该注意些什么,请举例说明

面试题1:

a = ['a','b','b','d','e']
s = ''     #可以写任意符号
print(s.join(a))
#输出为:abcde          如果写的是+符号,则输出为a+b+c+d+e

面试题2:

s = '','user','local','ngix',''
linuxPath = '/'.join(s)
print(linuxPath)
#输出为:/user/local/ngix/

windowPath = 'C:' + '\\'.join(s)
print(windowPath)
#输出为:C:\user\local\ngix\


#需要注意连接的必须都是字符串类型,否则会抛出异常。

#################################################
第七题:match函数的用法
面试题1:请简要描述python正则表达式中match函数的作用
面试题2:如果日期的格式是4位年,2位月,2位日(如2012-01-02),如何使用正则表达式判断一个字符串中是否包含这样的日期。

面试题1:

import re

re.match('hello','hello')

面试题2:

s = 'Today is 2013-12-12'
m = re.match('.*\d{4}-\d{2}-\d{2}.*',s)
print(m.group())

################################################
第八题:serch的用法
面试题1:请描述python正则表达式中的match和search的区别
面试题2:如果一个字符串中含有11位的手机号,请使用正则表达式找打第一个出现的手机号,并输出手机号,开始索引和结束索引

面试题1:
match:用于匹配(匹配满足正则表达式的字符串)
search:用于搜索(搜索字符串中的指定字符串),搜索满足条件的第一个字符串

面试题2:

import re
m = re.search('\d{10}','我的手机号是:17596996865')
if m is not None:
	print(m.group())
	print(m.start())
	print(m.end())

#############################################
第九题:正则表达式分组的使用
面试题1:如何搜索字符串中包含区号和分机号的第一个出现的电话号,并提取电话号中的区号,电话号和分机号,要求:
1.区号固定是三位数字
2.电话号至少是七位数字
3.分机号至少是三位数字
4.区号,电话号和分机号之间用连字符分隔
电话号案例:024-12345678-3456

import re
m = re.serch('(\d{3})-(\d{7,})-(\d{3,})','我的公司座机是:024-12345678-3456'

if m is not None:
	print(m.groups())		#元组形式显示
	print(m.groups()[0])	#区号
	print(m.groups()[1])	#电话号
	print(m.groups()[2])	#分机号

################################################
第十题:findall函数的使用
面试题1:用正则表达式查找字符串中所有的Email,并输出这些Email。要求:所有的Email域名必须是.com或.net的。而且不区分大小写。

import re
s = '我的Email地址是[email protected],他的是[email protected],它的是[email protected]'
p = '[0-9a-zA-Z]+@[0-9a-zA-Z]+\.'
a = re.findall(p + 'com|' + p + 'net',s,re.I)
print(a)

总结:findall函数用于搜索字符串中所有满足条件的字字符串,该函数的第一个参数用于指定正则表达式,第二个参数用于指定待匹配的字符串,第三个字符串用于指定选项,如:re.I表示忽略大小写。

##############################################
第十一题:subn函数的使用
面试题1:用正则表达式查找字符串中所有的浮点数,并格式化这些浮点数,保留小数点后2位,最后将格式化后的浮点数替换原来的浮点数,同时输出替换后的结果和替换的次数,要求用一条语句实现。

步骤:
1.表示浮点数的正则表达式:-?\d+(.\d+)?
2.格式化浮点数:format
3.如何替换原来的浮点数:sub/subn(并返回替换的次数)

sub和subn都用于替换字符串中所有符合条件的字字符串,但sub函数只返回替换后的结果,而subn返回一个元组,元组的第一个元素返回替换后的结果,第二个元素返回替换的次数。

import re

def fun(matched):
	return format(float(matched.group()),'0.2f')

result = re.subn('-?\d+(\.\d+)?','fun','Pi is 3.1415926,e is 2.71828183,-0.2 + 1.3 = 1.1')

#################################################
第十二题:1.findall函数的使用
2.Url的搜索
面试题1:提取HTML页面中所有的Url,要求:这些Url都属于a节点的Href属性。


import re 
s = '<a href="https://geekori.com">即刻起源</a> <a href="https:www.microsoft.com">微软</a>'

r = re.findall('<a[^>]*href="([^>])">',s,re.I)
print(r)

输出只有:
https://geekori.com
https:www.microsoft.com

发布了65 篇原创文章 · 获赞 50 · 访问量 3586

猜你喜欢

转载自blog.csdn.net/qq_44907926/article/details/104453182