前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python字符串的填充和计数

Python字符串的填充和计数

作者头像
Zkeq
发布2022-05-18 13:03:59
1K0
发布2022-05-18 13:03:59
举报
文章被收录于专栏:Zkeq

zfill 函数

功能

为字符串定义长度,如不满足,缺少的部分用 0 填充

用法

newstr = string.zfill(width)

参数

width: 新字符串希望的宽度

注意事项
  • 与字符串的字符无关
  • 如果定义长度小于当前字符串长度,则不发生变化
代码
代码语言:javascript
复制
# coding:utf-8

heart = 'love'


if __name__ == '__main__':
        print(' t  ' + heart)
        print('t    ' + heart)
        print(heart.zfill(10))
        print(heart.zfill(9))
        print(heart.zfill(8))
        print(heart.zfill(6))
        print(heart.zfill(4))

字符串的 count 函数

功能

返回当前字符串中某个成员 (元素) 的个数

用法

inttpe = string.count(item)

参数

item: 查询个数的元素

注意事项
  • 返回的是整形
  • 如果查询的成员 (元素) 不存在,则返回 0
代码语言:javascript
复制
# coding:utf-8

info = '''
    The mission of the Python Software Foundation is to promote, 
    protect, and advance the Python programming language, 
    and to support and facilitate the growth of a diverse and 
    international community of Python programmers.
'''

a = info.count('a')
b = info.count('b')
c = info.count('c')
d = info.count('d')
e = info.count('e')
f = info.count('f')

print(a, b, c, d, e, f)
number_list = [a, b, c, d, e, f]
print(number_list)
print('在列表中最大的数值是', max(number_list))

number_dict = {
            'a': a,
            'b': b,
            'c': c,
            'd': d,
            'e': e,
            'f': f,
}
print('每个成员对应的数值分别是', number_dict)

文章作者: Zkeq

文章链接: https://cloud.tencent.com/developer/article/2006115

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。 转载 请注明来自 Zkeq の Coding 日志

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-09-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • zfill 函数
    • 功能
      • 用法
        • 参数
          • 注意事项
            • 代码
            • 字符串的 count 函数
              • 功能
                • 用法
                  • 参数
                    • 注意事项
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档