扩展:

保留几位小数

保留两位小数

a = 12345.2487

print('%.2f' % a)
12345.25

保留三位小数

a = 12345.2487
print('%.3f' % a)

12345.249

一、占位符格式化输出

1.1 %s

%s可以接收任意类型的变量

name = 'sean'
age = 50

print('my name is %s,my age is %s.' % (name, age))  # 利用占位符格式化输出
my name is sean,my age is 50.

# 尝试调换括号里面的数值
print('my name is %s,my age is %s.' % (age, name))
my name is 50,my age is sean.  # 发现结果也更改了

1.2 %d

%d只能接受数字类型的变量

# 尝试接受字符串类型的变量
name = input('请输入你的名字:')
age = input('请输入你的年龄:')

print('my name is %d, my age is %d.' % (name, age))
请输入你的名字:kody
请输入你的年龄:21
Traceback (most recent call last):
 File "G:/python项目/test/test.py", line 5, in <module>
   print('my name is %d, my age is %d.' % (name, age))
TypeError: %d format: a number is required, not str

# 发现上面的接受字符串失败,再次尝试接受数字
name = 101
age = 21

print('my name is %d, my age is %d.' % (name, age))
my name is 101, my age is 21.  # 没有报错

二、.format()方式

name = input('请输入你的名字:')
age = input('请输入你的年龄:')

print('my name is {},my age is {}.'.format(name, age))
my name is kody,my age is 21.

# 尝试更换后面的name和age位置,看前面传入的值是否会调换位置
print('my name is {},my age is {}.'.format(age, name))
my name is 21,my age is kody.  # 值得位置确实调换了

# 思考,如何让值得位置不受name和age接收的限制?
print('my name is {names},my age is {ages}.'.format(ages=age, names=name))
请输入你的名字:kody
请输入你的年龄:21
my name is kody,my age is 21.

三、f-string

python3.6以后才有的新特性

name = input('请输入你的名字:')
age = input('请输入你的年龄:')

print(f'my name is {name}, my age is {age}.')
请输入你的名字:kody
请输入你的年龄:21
my name is kody, my age is 21.

python入门之格式化输出的更多相关文章

  1. Python 入门之格式化输出

    Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s --- 站字符串的位置(数字.字符串都能够进行填充) name = input('请输入姓名:') age = input('请 ...

  2. Python 3.x 格式化输出字符串 % & format 笔记

    Python 3.x 格式化输出字符串 % & format 笔记 python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记 %c字 ...

  3. python基础_格式化输出(%用法和format用法)(转载)

    python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...

  4. Python基础篇(格式化输出,运算符,编码):

    Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s  替换字符串      %d 替换整体数字  ...

  5. python基础_格式化输出(%用法和format用法)

      目录 %用法 format用法 %用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 1 >>> print('%o' % 2 ...

  6. Python进行JSON格式化输出,以及汉字显示问题

    格式化输出 转载地址  https://blog.csdn.net/real_tino/article/details/76422634 问题分析: Python下json手法的json在打印查看时, ...

  7. Python:Day05 格式化输出、列表

    注释:3个单引号或3个双引号 3个引号(单引或双引)还有另外一个作用:打印多行. msg = """hello 1 hello 2 hello 3"" ...

  8. python 基础之格式化输出

    字符占位符%s #_cvvh:"chenxi" #date: 2019/6/24 print ('chhjg') # 格式化输出 name = input("Name:& ...

  9. python - 字符串的格式化输出

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...

随机推荐

  1. Nuxt项目支持import写法的最新解决方案

    最近在看Nuxt开发vue项目的视频,视频中讲到Nuxt项目不支持es6的import写法.并提供了解决方案: 1.在package.json中添加我标红的部分: "scripts" ...

  2. Word Squares

    Description Given a set of words without duplicates, find all word squares you can build from them. ...

  3. LeetCode 1156. Swap For Longest Repeated Character Substring

    原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...

  4. MongoDB 查看当前配置

    MongoDB Enterprise > db.adminCommand({getParameter:"*"}) MongoDB Enterprise > db._ad ...

  5. 再谈System.BadImageFormatException

    今天,当我们继续学习.NET异常处理系列时,我们将查看System.BadImageFormatException.System.BadImageFormatException与GIF或JPG无关,而 ...

  6. swap file "*.swp" already exists!的解决方法

    Linux下编程难免要开启多个vim共同编辑同一个文件,这时再次保存就会出现: swap file "*.swp" already exists! [O]pen Read-Only ...

  7. Sublime Text 3安装Package Control并安装Processing插件

    由于PDE编辑界面对中文的支撑太差,于是想换到ST3来编辑代码,结果导致了噩梦的开始. 首先,找不到“Package Control”!!! 这还怎么玩~ 于是打开http://packagecont ...

  8. [技术博客]使用adb命令获取app(游戏)错误和警告日志

    adb命令的使用 直接在命令行中输入: adb logcat *:W 注意:这句命令的意思是显示所有优先级大于等于警告(Warning)的日志,查找崩溃问题一般用: adb logcat *:E 注意 ...

  9. mysql注入写文件

    select * from admin where id =-1 union select 1,'<?php phpinfo();?>',3,4 into outfile 'c:\\1.p ...

  10. git 相关流程和报错解决

    git 相关流程: 预先配置: gitroot $ git config user.name XXXgitroot $ git config user.email XXX@XXX.com git re ...