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

Python 自学小知识留存 2018年11月12日更新

2018-09-26 20:13 537 查看
版权声明:请勿随意转载复制,转载请注明地址出处 https://blog.csdn.net/songlh1234/article/details/82853539

 

1.Python format 格式化函数,format 函数可以接受不限个参数,位置可以不按顺序。

简单举例:更多参考http://www.runoob.com/python/att-string-format.html

2.Python 字典(Dictionary) keys()方法,用dict.keys()方法返回一个字典dict的所有键。

[code]test = {
"statusCode": 200,
"data": {
"totoal": "5",
"height": "5.97",
"weight": "10.30",
"age": "11"
},
"msg": "成功"
}
def get_keyvalue(data):
for key in data.keys():
key_value = data.get(key)
if isinstance(key_value, dict):
get_keyvalue(key_value)
else:
print('key='+str(key)+'  '+'value='+str(key_value))

print('直接使用data.keys()方法取出key'+str(test.keys()))
print('使用递归循环取出data.keys')
get_keyvalue(test)

3.Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False

4.Python isdigit()方法

判断字符串中的字符是否只由数字组成,如果是的话返回True,如果否返回False

5.Python isinstance(str, type)用法

判断一个对象是否是一个已知的类型isinstance (a,int);isinstance (a,str);isinstance (a,(str,int,list));isinstance (a,dict)等

6.python split()的用法,将字符串根据某个字符进行分割

[code]test = "aaa/bbbb"

print(test.split('/')[0])
print(test.split('/')[1])

7.python join()的用法,将序列中的元素以指定的字符连接生成一个新的字符串

[code]str = "-"
seq = ("a", "b", "c") # 字符串序列
print(str.join(seq))

list = ['1', '2', '3', '4', '5']
print(''.join(list))

 结果:

[code]a-b-c
12345

8.Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串

[code]str1 = "this is string example....wow!!!"

print(str1.ljust(50, '*'))

结果:this is string example....wow!!!******************

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: