• 技术文章 >常见问题 >Python常见问题

    python怎么让数字右对齐?

    yangyang2020-05-25 14:38:22原创9212

    python中使用format()方法格式化数字设置右对齐:< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

    >>> print('{} and {}'.format('hello','world')) # 默认左对齐

    hello and world

    >>> print('{:10s} and {:>10s}'.format('hello','world')) # 取10位左对齐,取10位右对齐

    hello and world

    >>> print('{:^10s} and {:^10s}'.format('hello','world')) # 取10位中间对齐

    hello and world

    >>> print('{} is {:.2f}'.format(1.123,1.123)) # 取2位小数

    1.123 is 1.12

    >>> print('{0} is {0:>10.2f}'.format(1.123)) # 取2位小数,右对齐,取10位

    1.123 is 1.12

    更多Python知识请关注Python自学网

    专题推荐:python
    品易云
    上一篇:python怎么判断库是否安装? 下一篇:python是一种编程语言吗?

    相关文章推荐

    • python怎么清除窗口计算历史?• python怎么手动安装库?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网