menu 喵雨の小屋
search assignment self_improvement

PYTHON学习实例2

MeowRain
MeowRain 2020年11月05日 阅读:178

方括号的使用

name = 'MGSY "bLOG " is very good'
print(name[0]) #第一个字符
print(name[1])#第二个字符
print(name[2])#第三个字符
print(name[-1])#从右面开始第一个字符
print(name[0:3])#从左起到第三个字符
print(name[:3])#从左起到第三个字符
print(name[:])#打印全部字符

格式化字符串

first = 'john'
last = 'smith'
message = f'{first} [{last}] is a coder' #格式化字符串
print(message)

查看输入信息长度 len

message = 'how many characters are there?'
print(len(message))

将英文大写 upper

print(message.upper())

HOW MANY CHARACTERS ARE THERE?

将英文小写 lower

eng = 'BIG ENGLISH IS HERE' #大写
print(eng.lower())

big english is here

找到字符串中字符在第几位 find

code = 'time is passing now '
print(code.find('p'))

替换一个值 replace

print(code.replace('time','money'))

判断某个字符串在不在变量中 in

print ('time' in code)#True
print ('money' in code)#False

完整代码

#查看输入信息长度 len
message = 'how many characters are there?'
print(len(message))

#将英文大写 upper
print(message.upper())
#HOW MANY CHARACTERS ARE THERE?

#将英文小写 lower
eng = 'BIG ENGLISH IS HERE' #大写
print(eng.lower())
#big english is here

#找到字符串中字符在第几位 find
code = 'time is passing now '
print(code.find('p'))

#替换一个值 replace
print(code.replace('time','money'))

#判断某个字符串在不在变量中 in
print ('time' in code)#True
print ('money' in code)#False

分类: 笔记
标签: python
目录