Python面试100讲-Chapter 3-优快云

Python面试100讲-优快云

Chapter 3:字符串与正则表达式

1. Python字符串格式化知多少

1. 在Python语言中有多少种格式化字符串的方法?

#1. %格式化
#2. 模板字符串
#3. 字符串的format方法
#4. fstring

2. 请解释什么是模板字符串,如何使用

# 通过Template对象封装,$防止一些占位符,并通过subsititue方法用实际的值替换这些占位符

from string import Template

template1 = Template('$s测试字符串,$s段没有意义')
print(template1.substitute(s='这'))
print(template1.substitute(s='那'))

template2 = Template('${h}ello world')
print(template2.substitute(h='l'))

template3 = Template("$$$dollar equals to $pound pounds")
print(template3.substitute(dollar=12,pound=12))
data = {}
data['dollar']=4
data['pound']=1
print(template3.substitute(data))
这测试字符串,这段没有意义
那测试字符串,那段没有意义
lello world
$12 equals to 12 pounds
$4 equals to 1 pounds

2. 使用fstring方式格式化字符串

1. 在Python语言中哪种格式化方式直接使用变量

fstring

2. 请用代码描述如何使用fstring格式化字符串

name = 'Bill'
age = 20

def getAge():
	return 21

s = f'He is {name}, age is {age}. {getAge()} years old next year'
print(s)
He is Bill, age is 20. 21 years old next year
class Person:
	def __init__(self):
		self.name = 'Mike'
		self.age=30
	def getAge(self):
		return self.age +1

person = Person()
s = f'He is {person.name}, age is {person.age}. {person.getAge()} years old next year'
print(s)
He is Mike, age is 30. 31 years old next year

3. 详细描述Python字符串的基本操作

1. 详细描述Python字符串的基本操作

#coding=utf-8
# 1. 通过索引获得字符
s1 = 'hello world'

print(s1[3])

# 2. 分片

print(s1[3:8])
print(s1[6:])
print(s1[::2])

# 3. 乘法

s2='xyz'
print(10*s2)

print('b' in s2)
print('y' in s2)
print('b' not in s2)

print(len(s1))

print(min(s2))
print(max(s2))
l
lo wo
world
hlowrd
xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz
False
True
True
11
x
z

4. 向字符串的format方法传递参数有几种方式

1. 向字符串的format方法传递参数有几种方式

三种
默认方式,传入的参数与{}一一对应,命名参数和位置参数

2. 详细描述字符串的format方法如何格式化字符串

#coding=utf-8

s1 = 'Today is {},the temperature is {} degree.'

print(s1.format('Tuesday',18))

s2 = 'Today is {day},the temperature is {temp} degree.'

print(s2.format(temp=-9,day='Monday'))

s3 = 'Today is {day},{},the temperature is {temp} degree.'
print(s3.format(1,day='Wednesday',temp=10))

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

print(s4.format('day',3,day='Wednesday',temp=10))


class Person:
	def __init__(self):
		self.name = 'Mike'
		self.age=30
	def getName(self):
		return self.name
person = Person()
s = 'He is {person.name}, age is {person.age}.'
print(s.format(person=person))
Today is Tuesday,the temperature is 18 degree.
Today is Monday,the temperature is -9 degree.
Today is Wednesday,1,the temperature is 10 degree.
Today is Wednesday,3,the day temperature is 10 degree.
He is Mike, age is 30.

5. 让字符串居中显示

1. 如何让字符串居中显示,有哪些方法

1. center方法
2. format方法

2. 请使用center方法让字符串居中显示,两侧显示#号

print('<'+'hello'.center(20)+'>')
print(len('hello'.center(20)))
print('hello'.center(20,'#'))

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

6. 连接列表中的分隔符

1. 如何将列表中的字符串类型的元素连接在一起(首尾相接)

a=['a','b','c','d','e']
s=''
print(s.join(a))
abcde

2. 字符串中的join方法的作用是什么,使用join应该注意些什么,请举例说明

只能连接字符串类型

7. 用正则表达式判断字符串中是否包含日期

1. 请简要描述Python正则表达式中match函数的作用

import re

print(re.match('.*hello','ahello'))

2. 如果日期的格式是4位年,2位月,2位日(2020-03-03),如何使用正则表达式判断一个字符串中是否包含这样的日期

import re

s="Today is 2012-12-20"

pattern=r'.*\d{4}-\d{2}-\d{2}.*'
m=re.match(pattern,s)

if m is not None:
	print(m.group())
Today is 2012-12-20

8. 寻找字符串中的手机号

1. 请描述正则表达式中match和search的区别

# match用于匹配,search用于搜索
import re

print(re.match('python','I love python'))
print(re.search('python','I love python'))
None
<_sre.SRE_Match object at 0x0000000001EAC510>

2. 如果一个字符串中含有11位手机号,请使用正则表达式找到第一个出现的手机号,并输出手机号,开始索引和结束索引

import re

pattern = r'1\d{10}'

m = re.search(pattern,'my phone number is 13200000000')
if m is not None:
	print(m.group())
	print(m.start())
	print(m.end())
13200000000
19
30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值