Python 分割路径、partition、sep、round、字符串填充
2022/3/5 9:15:08
本文主要是介绍Python 分割路径、partition、sep、round、字符串填充,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import os # os.path.split 分割路径 path1 = r'e:\tool\read.txt' print(os.path.split(path1)) # ('e:\\tool', 'read.txt') print(os.path.splitext(path1)) # ('e:\\tool\\read', '.txt') # partition 自定义字符分割 path2 = r'e:\tool\read.txt' print(path2.partition('\\')) # ('e:', '\\', 'tool\\read.txt') print(path2.rpartition('\\')) # ('e:\\tool', '\\', 'read.txt')
# sep 间隔 默认为空格 print('2022年', '1月', '31日') # 2022年 1月 31日 print('2022年', '1月', '31日', sep='->') # 2022年->1月->31日 # round 将float保留多少位小数 默认转为int 四舍五入 flot_num = 231.6543 print(round(flot_num)) # int 232 print(round(flot_num, 3)) # float 231.654 # pow(x,y,z) x的y次方,用z取模 print(2 ** 3) # 8 print(pow(2, 3)) # 8 print(pow(2, 3, 3)) # 2 # 字符串填充 print('abc'.zfill(5)) # 00abc print('abc'.ljust(5, '@')) # abc@@ print('abc'.rjust(5, '@')) # @@abc # 字典 get方法 键不存在时,返回None或自定义的值 dict_val = {'a': 1, 'b': 2} print(dict_val.get('c', 3)) # 3 # eval 把字符串当作python代码执行,并返回执行结果 eval_res = eval('[1,2]') print(type(eval_res), eval_res) # <class 'list'> [1, 2]
这篇关于Python 分割路径、partition、sep、round、字符串填充的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python