python zfill

Python String zfill()

Python字符串zfill()

Python String zfill(width) function returns a new string of specified width. The string is filled with 0 on the left side to create the specified width.

Python字符串zfill(width)函数返回指定宽度的新字符串。 该字符串在左侧用0填充以创建指定的宽度。

If the string starts with sign characters (+,-) then the padding is done after the sign. If the specified width is less than the original string, then the original string is returned.

如果字符串以符号字符(+,-)开头,则在符号之后进行填充。 如果指定的宽度小于原始字符串,则返回原始字符串。

Python字符串zfill() (Python String zfill())

Let’s look at some examples of zfill() function.

让我们看一下zfill()函数的一些示例。

s = '100'
print(s.zfill(6))s = '+100'
print(s.zfill(6))s = '-100'
print(s.zfill(6))

Output:

输出:

000100
+00100
-00100

Let’s see what happens when the specified width is smaller than the string length.

让我们看看当指定的宽度小于字符串长度时会发生什么。

s = '+100'
print(s.zfill(3))

Output: +100

输出: +100

This function is useful when the string is numeric in nature. However, it works with any non-numeric string too.

当字符串本质上是数字时,此功能很有用。 但是,它也适用于任何非数字字符串。

s = 'abc'
print(s.zfill(6))s = '+abc'
print(s.zfill(6))

Output:

输出:

000abc
+00abc
GitHub Repository.GitHub存储库中签出更多Python示例。

Official Documentation: zfill()

官方文档: zfill()

翻译自: https://www.journaldev.com/24546/python-string-zfill

python zfill

python zfill_Python字符串zfill()相关推荐

  1. Python入门--字符串内容对齐操作center,ljust,rjust,zfill

    #字符串内容对齐操作(类似于word左对齐,右对齐,居中对齐) #center()-->居中对齐,第1个参数指定宽度,第2个参数指定填充符,第二个参数是可选的,默认是空格 #如果设置宽度小于实际 ...

  2. python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...

    作者:骏马金龙 原文地址: https://www.cnblogs.com/f-ck-need-u/p/9127699.html python中字符串对象提供了很多方法来操作字符串,功能相当丰富. p ...

  3. Python之字符串的134个常用操作

    一.字符串切片操作 test = "Python Programming" print("String: ", test)# First one charact ...

  4. python的字符串内建函数

    python的字符串内建函数 字符串方法是从python1.6到2.0慢慢加进来的--它们也被加到了Jython中. 这些方法实现了string模块的大部分方法,如下表所示列出了目前字符串内建支持的方 ...

  5. python字符串填充_填充函数(Python)字符串.zfi

    我想更改下面的Python函数,以涵盖我的business_代码需要填充的所有情况.string.zfillPython函数处理此异常,将填充到左侧直到达到给定的宽度,但我以前从未使用过它.在#fun ...

  6. python打印字符串的前五行_python基础-字符串(6)

    一.引言 当打来浏览器登录某些网站的时候,需要输入密码,浏览器把密码传送到服务器后,服务器会对密码进行验证,其验证过程是把之前保存的密码与本次传递过去的密码进行对比,如果相等,那么就认为密码正确,否则 ...

  7. [转载] python判断字符串中包含某个字符串_干货分享| Python中最常用的字符串方法

    参考链接: Python中字符串string capitalize 字符串是字符序列.Python中内置的string类代表基于Unicode国际字符集的字符串.除了Python中常见的操作外,字符串 ...

  8. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...

    参考链接: python中的字符串string center 作者:骏马金龙 原文地址: https://www.cnblogs.com/f-ck-need-u/p/9127699.html pyth ...

  9. Python Str字符串 字符串常用方法 定义 创建 拆分 成分判断 大小写调整 格式化 填充 替换 访问 查找

    Python Str字符串 定义 文本序列类型-Python中的str文本数据是用str对象或字符串处理的.字符串是Unicode代码点的不可变序列.字符串文字的书写方式多种多样: 单引号:'allo ...

最新文章

  1. tps 数据库写并发衡量_MPP数据库简介
  2. printf格式化字符串用法
  3. python基础(part1)--注释/变量/del语句
  4. Linux怎么查看保存的密码,在Linux中查看已保存的WiFi密码
  5. 微课|中学生可以这样学Python(例5.8):凯撒加密
  6. 有关linux下find和xargs的使用
  7. puppet详解(六)——exec资源详解
  8. 有关字符串的算法(KMP,Manacher,BM)陆续补充
  9. IDEA集成SVN代码管理常用功能
  10. VLD在VS2019中的使用以及应用程序无法正常启动(0xc0150002)
  11. 2048游戏编程C语言,使用C语言编写一个2048游戏
  12. Learning Agile software Development
  13. for(int num:nums)
  14. 3年级计算机的知识能力,三年级信息技术教案资料三篇
  15. 基于SSM的快递代取管理系统
  16. 数据库的原理,一篇文章搞定(三)
  17. 儿童编程Scratch入门课程都学习什么内容?
  18. 【Java】微信企业付款报错:java.io.IOException: toDerInputStream rejects tag type 45
  19. 旧话重提,还来聊聊ERP
  20. unity_Text文本和图片组合

热门文章

  1. Git.gitignore规则不生效的解决办法
  2. codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)
  3. python之函数用法capitalize()
  4. Uva 140 Bandwidth
  5. 我要好offer之 二叉树大总结
  6. [转载] Python Set intersection() 方法
  7. [CTSC2008] 网络管理
  8. 生成一定数量的不重复随机数
  9. 为什么道理都懂,课执行力差的现象如此普遍?
  10. c 无回显读取字符/不按回车即获取字符