需求:

将数字格式化后输出,并控制数字的位数、对齐、千位分隔符和其他的细节
x = 1234.56789
# Two decimal places of accuracy
print(format(x, '0.2f')) # '1234.57' # Right justified in 10 chars, one-digit accuracy
print(format(x, '>10.1f')) # '1234.6' # Left justified
print(format(x, '<10.1f')) # '1234.6 ' # Centered
print(format(x, '^10.1f')) # 1234.6 # Inclusion of thousands separator
print(format(x, ',')) # '1,234.56789'
print(format(x, '0,.1f')) # '1,234.6'
二八十六进制整数
x=1234
print(bin(x)) # 0b10011010010
print(oct(x)) # 0o2322
print(hex(x)) # 0x4d2
# 去掉进制的前缀,可以用format
print(format(x,"b")) #
print(format(x,"o")) #
print(format(x,"x")) # 4d2

Python之数字的格式化输出的更多相关文章

  1. Python小代码_2_格式化输出

    Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...

  2. Python的三种格式化输出

    今天刚学了python的三种格式化输出,以前没接触过这么有趣的输出方式,现在来分享一下. #!/user/bin/env python#coding:utf-8#三种格式化输出 #第一种格式化输出na ...

  3. Java中数字的格式化输出

    Java中数字的格式化输出 double d = 345.678; String s = "hello!"; int i = 1234; //"%"表示进行格式 ...

  4. python - 用户交互/数据类型/格式化输出/运算符/流程控制单双多分支

    python:用户交互: 等用户输入,做反应: username=input("username:")password=input("password:")pr ...

  5. 2、python的print函数格式化输出

    1.格式化输出浮点数(float) pi = 3.141592653 print('%10.3f' % pi)  #字段宽10,精度3 ,结果 print('%010.3f' % pi)  #用0填充 ...

  6. Python 字符串拼接、格式化输出、深浅复制

    拼接:"+"号(同类型可拼接) >>>li = [1,2] >>>li + li [1,2,1,2] >>>li*2 [1,2 ...

  7. 04 Python之while循环/格式化输出/运算符/编码

    1. while循环 while 条件: 循环体(break,continue) else: 循环体(break,continue) break:彻底干掉一个循环,直接跳出. continue:停止当 ...

  8. Python基础一(格式化输出、流程控制)

    (1)格式化输出(%% 第一个% 转译) # 格式化 输入 输出 name = input("Name:") age = input("Age:") job = ...

  9. python print %s 号格式化输出

    python %号格式化输出: 一种字符串格式化的语法, 基本用法是将值插入到%s占位符的字符串中. %s,表示格式化一个对象为字符 "%±(正负号表示)3(数字表示字符串的长度)s&quo ...

随机推荐

  1. 解决chrome浏览器安装不上的问题

    1.  打开注册表:  windows键 + R --> 输入regedit -->  回车 (注:windows键在左ctrl附近微软图标的键) 2.  找到 32位:HKEY_LOCA ...

  2. Mac brew 安装Postgres 开机自启动

    以下所有命令在mac 终端执行 1.安装postgres brew install postgres 2.brew 安装的程序都可以在/usr/local/Cellar/下找到,去/usr/local ...

  3. Linux centos7安装git

    1.下载git wget https://github.com/git/git/archive/v2.14.1.zip 2.安装依赖 yum -y install zlib-devel openssl ...

  4. Ubuntu下使用天河注意事项

    把下载的.cgi登陆文件用文本编辑器打开,修改端口 用管理员权限打开.cgi才能绑定端口 $ gksu nautilus # browse files as root $ gksu gedit /et ...

  5. TortoiseGit操作之提交代码到远程库

    1.在本地代码库的文件夹中,"右键" 2.GIT提交要求必须填写Commit message,请认真填写描述信息. 建议填写的变更项编号,如上图. 代码提交到本地的配置库中,然后p ...

  6. selenium与appium怎样联系

    appium是手机app端的自动化,它继承了webdriver(也就是selenium 2)          不过appium仍然需要通过selenium最后做测试工具,但是appium起到了一个连 ...

  7. MySQL执行计划示例

    以上示例来自尚硅谷!

  8. 09-排序2 Insert or Merge(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. git merge 上线操作流程

    switch to branch git merge 主干到分支解决冲突,解决冲突文件,冲突文件,add to index 检查文件无<<<<<<< .=== ...

  10. bugku | 你从哪里来

    题目链接 之前一直以为要用x-forwarded-for ,谁道用的是referer,Orz.在此特地记录,x-forwarded-for 和 referer的区别 X-Forwarded-For(X ...