您的位置:首页 > 编程语言 > Python开发

Python学习笔记--day02

2018-01-16 21:28 453 查看
二进制转十进制
numStr = input("请您输入一个数字:")
numInt = int(numStr,base=2)
print(numInt)
打印字符串content = "0123456789"
index = 0
while index < len(content):
item = content[index]
print(item)
index += 1切片
name = "abcdef"
print(name[0:3])
print(name[0:5])
print(name[3:5])
print(name[2:])
print(name[1:-1])
print(name[:3])
print(name[0::2])
print(name[5:1:-1])
print(name[::-1])
结果:
abc
abcde
de
cdef
bcde
abc
ace
fedc
fedcba字符串的四种表示方法str1 = 'Hello World'
str2 = "Hello World"
str3 = '''Hello World ???'''
str4 = """Hello World !"""
首字母大写str1 = "hello world"
str2 = str1.capitalize()
print(str2)
将字母小写str1 = "Hello WORLD"
str2 = str1.casefold()
str3 = str1.lower()
print(str2)
print(str3)查询子字符串在字符串中出现的次数content = "helloworld"
count = content.count('o')
print(count)

content = "1234567890123456"
count = content.count("123",5,15)
print(count)
将字符串转bytes类型,或者bytes类型转字符串str1 = "中"
data = str1.encode('utf8')
print(data)
print(type(data))

str2 = data.decode("utf8")
print(str2)
print(type(str2))
以……开始,以……结束url = "www.baidu.com"
if not url.startswith("http"):
url = "http://" + url
print(url)

content = "www.baidu.com"
print(content.endswith('.com'))
find index的用法# find查找子字符串在字符串中第一次出现的位置
content = "0123456798012345679"
index = content.find('123')
print(index)

index2 = content.find('123',10)
print(index2)

# 如果找不着对应的位置,则返回-1
index3 = content.find("abc")
print(index3)

index1 = content.index("123")
print(index1)

# 如果找不着对应的位置,则报错
# index = content.index("abc")
# print(index2)

if "abc" in content:
index3 = content.index("abc")
print(index3)
else:
print("查无此数据")

index4 = content.find("abc")
if index4 == -1:
print("查无此数据")
else:
print(index4)格式化字符串
price = 29.8534343
address = "南京"
content = "价格:{:.3f} 产地:{}".format(price,address)
print(content)

priceUnit = "500g"
content = "价格:{:.3f}/{} 产地:{}".format(price,priceUnit,address)
print(content)

content = "价格:{0:.3f}/{2} 产地:{1}".format(price,address,priceUnit)
print(content)
判断是否是十进制数字
num = input("请您输入一个数字:")
print(num.isdecimal())
判断是否是小写print("abc".islower())判断是否是大写print("ABC".isupper())
将字符串转换为大写形式
print("abc".upper())
将字符串转换为小写形式print("ABC".lower())
split使用字符串分割原来的字符串
content = "hello world ni hao a"
data = content.split(" ")
print(data)
print(type(data))
join将列表中的数据转换为字符串data = ["this" ,"is","my","first","idea"]
content = "\t".join(data)
print(content)获取网页上的图片import requests
url = "http://www.baidu.com/img/bd_logo1.png"
response = requests.get(url)
data = response.content
names = url.split("/")
name = names[-1]
print(name)
with open(name,'wb') as f:
f.write(data)strip滤空
userName = input("请输入您的用户名:")
pwd = input("请输入您的密码:")
userName = userName.strip()
pwd = pwd.strip()
if userName == "lisi" and pwd == "123456":
print("登录成功")
else:
print("登录失败")
列表# 列表定义格式
list1 = list()
print(list1)
list2 = []
print(list2)
list3 = [11,22,33,"lkdffksj"]
print(list3)
list4 = list(list3)
print(list4)

# 添加数据
list1 = [11,22,33,44]
list1.append(55)
print(list1)

# 追加多个数据
list1 = [11,22,33,44]
list2 = [55,66,77,88,99]
list1.extend(list2)
print(list1)
print(list2)

# 指定的位置添加指定的元素
list1 = [11,22,33,44,55]
list1.insert(2,66)
print(list1)

# 清空数据
list1 = [11,22,33,44]
list1.clear()
print(list1)

# 删除指定下表的数据
list1 = [00,11,22,33,44,55]
removedItem = list1.pop()
print(removedItem)
print(list1)

removedItem = list1.pop(1)
print(removedItem)
print(list1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python