2.类型转换
## 2.类型转换
print('hello'.upper()) ##HELLO
print('HELLO'.lower()) ## hello
print('HELLO world'.title()) ## Hello World
print('HELLO world'.capitalize()) ## Hello world
print('HELLO world'.swapcase()) ##hello WORLD
实例:
# 需求:用户输入Y或者y都继续执行代码
# yum install httpd
choice = input ('是否继续安装程序(y|Y):')
if choice.lower() == 'y':
print("正在安装程序.....")正在安装程序
3.字符串开头和结尾的判断
# startswith
url = 'http://www.baidu.com'
if url.startswith('http'):
print(f'{url}是一个正确的网址,可以爬取网站')
# endswith:
# 常用场景:判断文件类型
filename = 'sun.png'
if filename.endswith('.png'):
print(f'{filename} 是图片文件')
elif filename.endswith('.mp3'):
print(f'{filename}是音乐文件')
else:
print(f'{filename}是未知文件')